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

/*
 *      configurationCB.c -- REALmagic calibration application 
 *
 *      Copyright (C) 2000 Sigma Designs
 *                    written by Nicolas Vignal <nicolas_vignal@sdesigns.com>
 *                    modified by Emmanuel Michon <emmanuel_michon@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 "configurationCB.h"
#include "autocalibration.h"
#include "resetdefault.h"
#include "adjustborder.h"

void AdjustBorder_clicked( GtkWidget *widget,gpointer   data ){
	DWORD FMPreturn;

	GtkWidget* AdjustBorder;

	AdjustBorder=create_BorderAdjust();

	gtk_window_set_transient_for (GTK_WINDOW (AdjustBorder),GTK_WINDOW (gtk_widget_get_toplevel(widget)));

	gtk_grab_add (AdjustBorder);
  gtk_widget_show (AdjustBorder);

  // Set mode to overlay
	FMPreturn=FMPAnalogOverlay(FMP_ANALOG_OVERLAY_SET,FMP_ANALOG_OVERLAY_ATTRIBUTE_MODE,1,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG);
	G_MESSAGE("set mode to overlay return: %ld",FMPreturn);

}



void AutoCalibration_clicked( GtkWidget *widget,gpointer   data ){

	GtkWidget* ConfigurationInfo;

	ConfigurationInfo=create_ConfigurationInfo();
	gtk_window_set_transient_for (GTK_WINDOW (ConfigurationInfo),GTK_WINDOW (gtk_widget_get_toplevel(widget)));
	gtk_grab_add (ConfigurationInfo);
  gtk_widget_show (ConfigurationInfo);

}



void ResetDefaults_clicked( GtkWidget *widget,gpointer   data ){

	GtkWidget* ResetDefaults;

	ResetDefaults=create_ResetDefaults();
	gtk_window_set_transient_for (GTK_WINDOW (ResetDefaults),GTK_WINDOW (gtk_widget_get_toplevel(widget)));
	gtk_grab_add (ResetDefaults);
  gtk_widget_show (ResetDefaults);

}



void Advanced_clicked( GtkWidget *widget,gpointer   data ){
	G_MESSAGE("Advanced clicked");
}



void C_Ok_clicked( GtkWidget *widget,gpointer   data ){
	GtkAdjustment	*obj;
  	GtkWidget *window;
	DWORD FMPreturn = 0;
	DWORD xresolution;
	DWORD yresolution;
	DWORD depth;
	DWORD freq;

	window=gtk_widget_get_toplevel(widget);

	G_MESSAGE("Ok clicked\nSave parameters");

  //	save contrast
	obj=GTK_RANGE(gtk_object_get_data (GTK_OBJECT (window), "contrast"))->adjustment;
	//FMPreturn=FMPWriteProfileWord(RMREG_CONTRAST,1000-(DWORD)obj->value);
	G_MESSAGE("Save contrast return %ld",FMPreturn);

	// save saturation
	obj=GTK_RANGE(gtk_object_get_data (GTK_OBJECT (window), "saturation"))->adjustment;
	//FMPreturn=FMPWriteProfileWord(RMREG_SATURATION,1000-(DWORD)obj->value);
	G_MESSAGE("Save saturation return %ld",FMPreturn);

	// save brightness
	obj=GTK_RANGE(gtk_object_get_data (GTK_OBJECT (window), "brightness"))->adjustment;
	//FMPreturn=FMPWriteProfileWord(RMREG_BRIGHTNESS,1000-(DWORD)obj->value);
	G_MESSAGE("Save brightness return %ld",FMPreturn);

	// save volume
	obj=GTK_RANGE(gtk_object_get_data (GTK_OBJECT (window), "volume"))->adjustment;
	//FMPreturn=FMPWriteProfileWord(RMREG_VOLUME,100-(DWORD)obj->value);
	G_MESSAGE("Save volume return %ld",FMPreturn);

 	// save balance
	obj=GTK_RANGE(gtk_object_get_data (GTK_OBJECT (window), "balance"))->adjustment;
	//FMPreturn=FMPWriteProfileWord(RMREG_BALANCE,(DWORD)(100+obj->value));
	G_MESSAGE("Save balance return %ld",FMPreturn);


	getres(&xresolution, &yresolution, &depth, &freq);
	FMPreturn=SaveParamForThisResolution (xresolution, yresolution, depth,freq);
	G_MESSAGE("SaveParamForThisResolution  return %ld",FMPreturn);

  gtk_main_quit();
}


void volume_changed( GtkAdjustment *volume, GtkAdjustment *balance)
{
	updateAudio((int)volume->value,(int)balance->value);
}



void balance_changed( GtkAdjustment *balance,GtkAdjustment *volume)
{
	updateAudio((int)volume->value,(int)balance->value);
}



void brightness_changed( GtkAdjustment *adj,gpointer   data )
{
//	DWORD FMPreturn;

	G_MESSAGE("Brightness changed new brightness %d",1000-(unsigned int)adj->value);
	//FMPreturn=FMPSet(FMPI_BRIGHTNESS,1000-(unsigned int)adj->value);
	G_MESSAGE("set new brightness return: %ld",FMPreturn);
}



void contrast_changed( GtkAdjustment *adj,gpointer   data )
{
//	DWORD FMPreturn;

	G_MESSAGE("Contrast changed new contrast %d",1000-(unsigned int)adj->value);
	//FMPreturn=FMPSet(FMPI_CONTRAST,1000-(unsigned int)adj->value);
	G_MESSAGE("set new contrast return: %ld",FMPreturn);
}



void saturation_changed( GtkAdjustment *adj,gpointer   data )
{
//	DWORD FMPreturn;

	G_MESSAGE("Saturation changed new saturation %d",1000-(unsigned int)adj->value);
	//FMPreturn=FMPSet(FMPI_SATURATION,1000-(unsigned int)adj->value);
	G_MESSAGE("set new saturation return: %ld",FMPreturn);
}

void loadconfig( GtkWidget *widget)
{
	GtkAdjustment	*obj;
	DWORD xresolution;
	DWORD yresolution;
	DWORD depth;
	DWORD freq;

	G_MESSAGE("Ok clicked\nLoad parameters");

	// load contrast
	obj=GTK_RANGE(gtk_object_get_data (GTK_OBJECT (widget), "contrast"))->adjustment;
	gtk_adjustment_set_value(obj,1000-500/*FMPGetProfileWord(RMREG_CONTRAST)*/);

	// load saturation
	obj=GTK_RANGE(gtk_object_get_data (GTK_OBJECT (widget), "saturation"))->adjustment;
	gtk_adjustment_set_value(obj,1000-500/*FMPGetProfileWord(RMREG_SATURATION)*/);

	// load brightness
	obj=GTK_RANGE(gtk_object_get_data (GTK_OBJECT (widget), "brightness"))->adjustment;
	gtk_adjustment_set_value(obj,1000-500/*FMPGetProfileWord(RMREG_BRIGHTNESS)*/);

	// load balance
	obj=GTK_RANGE(gtk_object_get_data (GTK_OBJECT (widget), "balance"))->adjustment;
	gtk_adjustment_set_value(obj,(int)(/*FMPGetProfileWord(RMREG_BALANCE)*/200-100));

	// load volume
	obj=GTK_RANGE(gtk_object_get_data (GTK_OBJECT (widget), "volume"))->adjustment;
	gtk_adjustment_set_value(obj,100-50/*FMPGetProfileWord(RMREG_VOLUME)*/);

 	getres(&xresolution, &yresolution, &depth, &freq);
	LoadParamForThisResolution (xresolution, yresolution, depth,freq);
}


void updateAudio(int volume, int balance)
{
	DWORD volumeleft;
  DWORD volumeright;
	DWORD percentleft;
  DWORD percentright;

  DWORD FMPreturn;

	volumeleft=FMPGet(FMPI_LEFT_VOLUME);
	volumeright=FMPGet(FMPI_RIGHT_VOLUME);

	G_MESSAGE("Old volume :\nleft : %ld right : %ld",volumeleft,volumeright);
	G_MESSAGE("Balance : %d Volume %d",balance,volume);

	if (balance<=0){
		percentleft=100;
		percentright=100+balance;
	}
	if (balance>0){
		percentright=100;
		percentleft=100-balance;
	}

	volumeleft =(100-volume)*percentleft/100;
	volumeright=(100-volume)*percentright/100;
	
	G_MESSAGE("New volume :\nleft : %ld right : %ld",volumeleft,volumeright);

	FMPreturn=FMPSet(FMPI_LEFT_VOLUME,volumeleft);

#ifdef DEBUG
	G_MESSAGE("Set new volume left return %ld",FMPreturn);
	FMPreturn=FMPGet(FMPI_LEFT_VOLUME);
	G_MESSAGE("Get new volume left return %ld",FMPreturn);
#endif
	FMPreturn=FMPSet(FMPI_RIGHT_VOLUME,volumeright);

#ifdef DEBUG
	G_MESSAGE("Set new volume right return %ld",FMPreturn);
	FMPreturn=FMPGet(FMPI_RIGHT_VOLUME);
	G_MESSAGE("Get new volume right return %ld",FMPreturn);
#endif
}
