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

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

// sorry BOOL and BYTE conflict with FMP types
Bool XF86VidModeGetViewPort(Display *display,int screen,int *x_return,int *y_return);

Display *dpy;
int scr;

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 autocalibrate( )
{
	
	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);


	getres(&xsize,&ysize,&depth,&freq);
	FMPreturn=SaveParamForThisResolution(xsize,ysize,depth,freq);
	// 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);
}


int main( int   argc,
          char *argv[] )
{
  DWORD FMPreturn;
  int scrcount;


  dpy = XOpenDisplay (NULL);
  if (!dpy) exit(1);
  
  scrcount=ScreenCount(dpy);
  if (scrcount!=1)
	  fprintf(stderr,"Warning: screen count is %d. Continuing anyways.\n",scrcount);

  scr=DefaultScreen(dpy);

  {
	  DWORD x,y,depth,freq,rootx,rooty;
	  int xof,yof;

	  // don't mismatch Screen *Scr and int scr.
	  Screen *Scr=DefaultScreenOfDisplay(dpy);
	  
	  rootx=WidthOfScreen(Scr);
	  rooty=HeightOfScreen(Scr);
	  
	  if (!XF86VidModeGetViewPort(dpy,scr,&xof,&yof)) exit(1);
	  
	  getres(&x,&y,&depth,&freq);
	  
	  if ((x!=rootx)||(y!=rooty)) 
		  fprintf(stderr,"Warning: you are using a viewport.\n"
			  "         (root window is %ldx%ld on actual monitor %ldx%ld, offset is (%d,%d)).\n"
			  "         Let's try to keep the offset to (0,0).\n",
			  rootx,rooty,x,y,xof,yof);
  }
  

  // Connect to the server
	FMPConnectToMSGQUEUE();
	FMPOpen(FMPF_SYSTEM,32*1024,8,NULL,0);
	waitForAnalogOverlayAccess();
	// FMPPlay();
	// Test the VGA cable
	if (!FMPAnalogOverlay(FMP_ANALOG_OVERLAY_GET,FMP_ANALOG_OVERLAY_ATTRIBUTE_OVERLAY_CABLE_PRESENT,
			     UNUSED_ARG,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG)) {
	
	G_MESSAGE("Please attach VGA cable.\n");
	exit(1);
	}

	// 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);	

	autocalibrate();
	
  // FMPStop();
  FMPClose();
  MPEGDriverUnload();
  return 0;
}

