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

/*
 *      loadparam.c -- Load analog overlay settings from file
 *
 *      Copyright (C) 2000 Sigma Designs
 *                    written by Nicolas Vignal <nicolas_vignal@sdesigns.com>
 *                    modified by Emmanuel Michon <emmanuel_michon@sdesigns.com>
 *                                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 "analogstruct.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

void littleprint(analogstruct *analogparam){
int i;

	i=0;
	while (analogparam->paramtable[i].text != NULL){
	  printf("%s = %d\n",analogparam->paramtable[i].text,(int)analogparam->paramtable[i].value);
	  i++;
	}
}

// try to load and set the analog overlay parameters for this resolution
// return 0 if the resolution exist in the .realmagicanalogoverlayrc file
// and set up the parameters on the card
// You MUST connect the novaserver before using this function
// return :
// 1 : cannot open the file
// 2 : cannot find the resolution

DWORD LoadParamForThisResolution(DWORD xresolution, DWORD yresolution, DWORD depth, DWORD freq)
{
  DWORD dwReturn;
  analogstructlist *thelist=NULL;

	dwReturn=LoadAllResolutions(&thelist);
  if (!dwReturn){
  	dwReturn=SetThisResolution(thelist,xresolution,yresolution,depth,freq);
  	CloseAllResolutions(thelist);
  }

	return dwReturn;
}

void CloseAllResolutions(analogstructlist *thelist){

  while (thelist != NULL){
	  CloseParamTable(&thelist->object);
    free(thelist);
    thelist=thelist->next;
  }
}


DWORD SetThisResolution(analogstructlist *thelist, DWORD xresolution, DWORD yresolution, DWORD depth, DWORD freq){

  DWORD dwReturn=0;
  DWORD found=0;
  int i=0;
  	
	while((thelist != NULL) && !found){
		if (thelist->object.XRESOLUTION == xresolution && thelist->object.YRESOLUTION == yresolution &&
    		thelist->object.DEPTH == depth && thelist->object.FREQUENCY == freq){
			found=1;
			
		  // set the display properties
    	FMPAnalogOverlay(FMP_ANALOG_OVERLAY_SET,FMP_ANALOG_OVERLAY_ATTRIBUTE_DISPLAYRESOLUTION,depth,xresolution,yresolution,UNUSED_ARG);
  		
			while (thelist->object.paramtable[i].text != NULL){
        FMPAnalogOverlay(FMP_ANALOG_OVERLAY_SET,thelist->object.paramtable[i].attribute,thelist->object.paramtable[i].value,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG);
  	    i++;
  		}
			
		}else {
		  thelist=thelist->next;
		}
	}
  if (found==0){
  	dwReturn=2;
  }
	
	return dwReturn;
}

DWORD LoadAllResolutions(analogstructlist **thelist){

  FILE *fd;
 	DWORD dwValue;
 	DWORD dwParse;
 	char sRegFile[MAX_REG_LINE ];
	char sBuffer[MAX_REG_LINE];
	char sTemp[MAX_REG_LINE];
	int xresolution=0;
	int yresolution=0;
	int depth=0;
	int freq=0;
	int i=0;
  analogstructlist *newelement;
  analogstructlist *current;
		
	strcpy( sRegFile,getenv( "HOME" ) );
	strcat( sRegFile,"/");
	strcat( sRegFile,ANALOGREGISTRYFILE);
#ifdef _BARBADOS_
	// adding as last character the decoder number.
	// REALmagicdevice is present in libnovaclient.so.
	strcat(sRegFile,REALmagicdevice+strlen(REALmagicdevice)-1);
#endif _BARBADOS_
	
	fd = fopen( sRegFile,"r");
	if (  fd == NULL )  {
		return 1;
	}

	current=*thelist;
  		
	while ( (feof(fd) == 0)){
  	fgets( sBuffer, MAX_REG_LINE,fd );
	  dwParse=1;
  	
    if ( (strchr(sBuffer,'['))!= NULL ){
      if(sscanf( sBuffer,"[ %u x %u x %u x %u ]",&xresolution ,&yresolution ,&depth ,&freq) == 4){
        newelement=( analogstructlist *)malloc(sizeof(analogstructlist));
	  		newelement->next=NULL;
				if (*thelist ==NULL){
		  	  current=newelement;
				  *thelist=current;
				}else {
			    current->next=newelement;
				  current=newelement;
				}
				InitParamTable(&current->object);
    	
				current->object.XRESOLUTION=xresolution;
				current->object.YRESOLUTION=yresolution;
				current->object.DEPTH=depth;
				current->object.FREQUENCY=freq;
				
    	  while( dwParse && (feof(fd) == 0)){
	    	  fgets( sBuffer, MAX_REG_LINE,fd );
 		      if ((dwParse=Reg_ParseEntry( sBuffer, sTemp , &dwValue))){
 			      i=0;
  		      while (current->object.paramtable[i].text != NULL){
  		 		    if (strcmp(sTemp,current->object.paramtable[i].text)==0){
  		   		    current->object.paramtable[i].value=dwValue;
    		   	  }
  	  		    i++;
  		   	  }
    		  }
    		}
    	}
    }
  }
  return 0;
}


/****f* RMREG/CReg_ParseEntry
* USAGE
*	BOOL Reg_ParseEntry( char* sLine, char *sKey, DWORD* pdwVal )
* DESCRIPTION
*	Parse a line of plain text, looking for an expression of the following format
*		"%s = %ud"
*	'#' is used as comment qualifier.
*	returns 1 and affect related variables if it was find.
*	returns 0, an empty string and a 0xFFFFFFFF otherwise.	
* PARAMETERS
*	char* sLine : The line to be parsed.
* 	char *sKey 	: a string long enough to hold the name of the key.
*	DWORD* pdwVal   : a pointer to a dword receiving the value read from the line.
* SEE ALSO
*
********************************************************************************************/

DWORD Reg_ParseEntry( char* sLine, char *sKey, DWORD* pdwVal )
{
     	DWORD  dwValTemp;
     	char sKeyTemp[MAX_REG_LINE];
     	int iFields;
     	char sLineCut[MAX_REG_LINE];
     	char *pChar;
     	
     	strcpy(sKey,"");
     	*pdwVal = 0xFFFFFFFF;
     	
     	if ( sLine == NULL || sKey == NULL ||  pdwVal == NULL )
     		return 0;

     	// cut the line to ( 256 ) to limit the likelyness of an overflow	     		
     	strncpy( sLineCut, sLine, MAX_REG_LINE );
     	
     	// remove any comment
     	pChar = strchr(sLineCut,'#')	;
     	if ( pChar != NULL )
     		*pChar = 0x00;
     	
     	// no = sign then we do not care
     	pChar = strchr(sLineCut,'=')	;
     	if ( pChar == NULL )
     		return 0;
     	
     		
     	iFields = sscanf( sLineCut,"%s = %ud", sKeyTemp, (unsigned int *) &dwValTemp );
     	if ( iFields != 2 )
     		return 0 ;
     	
     	strcpy(sKey,sKeyTemp);
     	*pdwVal = dwValTemp;
     		
     	return 1;	
     	
}     	


