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

/*
 *      autocalibrationCB.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 "autocalibrationCB.h"
#include "allblackbut.h"

DWORD xsize;
DWORD ysize;

void screen1(void)
{
	AllBlackButPattern(MY_DEEPBLUE,0,0,355,1);
}

void screen2(void)
{
	AllBlackButPattern(MY_DEEPBLUE,0,0,2,288);
}

void screen3(void)
{
	AllBlackButPattern(MY_DEEPBLUE,355,0,2,288);
}

void screen4(void)
{
	AllBlackButPattern(MY_WHITE,(xsize-200)/2,0,200,ysize);
}

void screen5(void)
{
	AllBlackButPattern(MY_GREY,(xsize-200)/2,0,200,ysize);
}

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

	DWORD depth;
	DWORD freq;	

 	G_MESSAGE("Ok clicked Start Calibration");

 	// switch output to VGA
	FMPreturn = FMPSet (FMPI_VIDEOOUT, FMPV_VIDEOOUT_VGA);
	G_MESSAGE("Switch to output VGA return: %ld",FMPreturn);

	AllBlackButInit();

 	if(getres(&xsize,&ysize,&depth,&freq)){
		g_warning("unable to get the current resolution");
		return;
	}

	G_MESSAGE("Current resolution :    %ldx%ld pixels, depth %ld bpp at %ld Hz",xsize, ysize,depth,freq);
			
	if (depth<16){
		g_warning("Autocalibration needs depth > 8 bpp");
		return;
	}

	if (xsize<640 && ysize<480){
		g_warning("Minimum resolution supported : 640x480");
		return;
	}
	
	// set the display properties
	LoadParamForThisResolution( xsize, ysize, depth, freq);
	FMPreturn=FMPAnalogOverlay(FMP_ANALOG_OVERLAY_SET,FMP_ANALOG_OVERLAY_ATTRIBUTE_DISPLAYRESOLUTION,depth,xsize,ysize,UNUSED_ARG);
	G_MESSAGE("set the display properties return : %ld",FMPreturn);	

	// Set mode to overlay
	FMPreturn=FMPAnalogOverlay(FMP_ANALOG_OVERLAY_SET,FMP_ANALOG_OVERLAY_ATTRIBUTE_MODE,2,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG);
	G_MESSAGE("set mode to overlay return: %ld",FMPreturn);	
				
	screen1();
	G_MESSAGE("pattern1 displayed");
  FMPreturn=FMPAnalogOverlay(FMP_ANALOG_OVERLAY_AUTOCALIBRATION,0x88,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG);		
	G_MESSAGE("Autocalibration YOffset return : %ld",FMPreturn);
		
	screen2();
	G_MESSAGE("pattern2 displayed");
	FMPreturn=FMPAnalogOverlay(FMP_ANALOG_OVERLAY_AUTOCALIBRATION,0x89,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG);		
	G_MESSAGE("Autocalibration XOffset return : %ld",FMPreturn);
				
	screen3();
	G_MESSAGE("pattern3 displayed");
	FMPreturn=FMPAnalogOverlay(FMP_ANALOG_OVERLAY_AUTOCALIBRATION,0x8a,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG);		
	G_MESSAGE("Autocalibration Correction return : %ld",FMPreturn);

	screen4();
	G_MESSAGE("pattern5 displayed");
	FMPreturn=FMPAnalogOverlay(FMP_ANALOG_OVERLAY_AUTOCALIBRATION,0x80,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG);		
	G_MESSAGE("Autocalibration white return : %ld",FMPreturn);

	screen5();
	G_MESSAGE("pattern4 displayed");
	FMPreturn=FMPAnalogOverlay(FMP_ANALOG_OVERLAY_AUTOCALIBRATION,0x82,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG);		
	G_MESSAGE("Autocalibration grey return : %ld",FMPreturn);

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

 	// switch output to VGA
	FMPreturn = FMPSet (FMPI_VIDEOOUT, FMPV_VIDEOOUT_VGA);
	G_MESSAGE("Switch to output VGA return: %ld",FMPreturn);

	window=gtk_widget_get_toplevel(widget);
	gtk_widget_destroy(window);
}


/* close the window */
void CI_Cancel_clicked( GtkWidget *widget,gpointer   data ){
	
	GtkWidget *window;

	window=gtk_widget_get_toplevel(widget);
  gtk_widget_destroy(window);
}
