#include <windows.h>
#include "resource.h"

BOOL UpdateDisplayRegistry(DWORD degree);
extern void DeleteTempRegistryFile();

extern BOOL WriteADSLOADRegistry(void);

BOOL CALLBACK RotateDlgProc(HWND hwndDlg,
                           UINT message,
                           WPARAM wParam,
                           LPARAM lParam)
{
    UINT nID;

    switch(message)
    {
    case WM_INITDIALOG:

        return (FALSE);

    case WM_COMMAND:
        nID = LOWORD(wParam);
        switch(nID)
        {
		case ID_0DEGREE:
			RETAILMSG(1,(L" No Rotate \r\n"));
			UpdateDisplayRegistry(0);
			WriteADSLOADRegistry();
			DeleteTempRegistryFile();
			break;			
		case ID_90DEGREE:
			RETAILMSG(1,(L" 90 Rotate \r\n"));
			UpdateDisplayRegistry(90);
			WriteADSLOADRegistry();
			DeleteTempRegistryFile();
			break;
		case ID_180DEGREE:
			RETAILMSG(1,(L" 180 Rotate \r\n"));
			UpdateDisplayRegistry(180);
			WriteADSLOADRegistry();
			DeleteTempRegistryFile();
			break;
		case ID_270DEGREE:
			RETAILMSG(1,(L" 270 Rotate \r\n"));
			UpdateDisplayRegistry(270);
			WriteADSLOADRegistry();
			DeleteTempRegistryFile();
			break;

        case IDCANCEL:
            EndDialog(hwndDlg, nID);
            break;

        default:
            break;
        }   //End of switch(nID) block
		
		EndDialog(hwndDlg, nID);
        return (TRUE);

    default:
        return (FALSE);

    }       //End of switch(message) block

	return (FALSE);

}
static char sRotate[]="Rotate";
static char DisplayRegKey[] = "[HKEY_LOCAL_MACHINE\\Drivers\\Display]";

static char TouchRegKey[] = "[HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\TOUCH]";
static char CalibRegKey[] = "CalibrationData";

extern char fLoadReg[];		//"\\Temp\\adsload.reg";
extern TCHAR fLoadRegW[];	//(TEXT("\\Temp\\adsload.reg"));
extern char fxLoadReg[];	// "\\Temp\\load.reg";
extern TCHAR fxLoadRegW[];	//(TEXT("\\Temp\\load.reg"));


BOOL UpdateDisplayRegistry(DWORD degree)
{
	int i=0;
	FILE *hFFX;
	FILE *hFFX1;

	DWORD temp=0;
	DWORD FileSize=0;
	DWORD dwBytesRead=0;

	char  fgetData[256];
	DWORD dwBytesWritten=0;
	char  *pSrch=NULL;

	BOOL  MainKeyFound = FALSE;
	BOOL  SubKeyFound = FALSE;
	BOOL  MainKeyExist = FALSE;

	BOOL  TouchKeyFound = FALSE;

	HKEY hKey;
	DWORD dwDisp;

#ifdef DEV_STATUS
	TCHAR TempNameW[256];
#endif
	DWORD TempLen;


	DEBUGMSG(1,(L" UpdateDisplayRegistry : Entry \r\n"));

#if _01_02_20001_
	if(RegDeleteKey(HKEY_LOCAL_MACHINE , TEXT("HARDWARE\\DEVICEMAP\\TOUCH")) != ERROR_SUCCESS)
		MessageBox(NULL, TEXT("Could not delete key!"), TEXT("Delete Error"), MB_OK); 
#else

	if(RegCreateKeyEx( 	HKEY_LOCAL_MACHINE, 
						TEXT("HARDWARE\\DEVICEMAP\\TOUCH"), 
						0,NULL,	0, 0,NULL,
						&hKey,
						&dwDisp) !=  ERROR_SUCCESS)
	{
		MessageBox(NULL,TEXT("Could not create key"), TEXT("Create Error"), 	MB_OK|MB_ICONEXCLAMATION);
		return FALSE;
	}

	RegDeleteValue(hKey, TEXT("CalibrationData")); 

#endif


	hFFX = fopen(fLoadReg,"r+t");

	if (hFFX == NULL) 
	{
		RETAILMSG(1,(L" Could not open File %s.\r\n",fLoadRegW));
		return FALSE;
	} 

	hFFX1 = fopen(fxLoadReg,"w+t");

	if (hFFX1 == NULL) 
	{
		RETAILMSG(1,(L" Could not open Flash FFX1.\r\n"));
		return FALSE;
	} 

	
	while( !feof( hFFX ) )
	{
		if( fgets(fgetData,256,hFFX) == NULL)
		{
			break;
			DEBUGMSG(1,(L"fgets error \r\n" ));
		}
		else
		{
			TempLen=strlen(fgetData);

			if(fgetData[0] == ';')
			{
				//dwBytesWritten = fwrite( fgetData, sizeof( char ), TempLen, hFFX1 );			
				fprintf( hFFX1, "%s",fgetData);

#ifdef DEV_STATUS
				atow(fgetData,TempNameW,TempLen);
				RETAILMSG(1,(L"%s\r\n", TempNameW));
#endif

				continue;
			}

			//
			// Skip to write TOUCH and CalibrationData registry keys
			//

			pSrch = strstr( fgetData, TouchRegKey);

			if( pSrch != NULL)	
			{
				TouchKeyFound = TRUE;
				continue;
			}

			if(TouchKeyFound)
			{
				pSrch = strstr( fgetData, CalibRegKey);

				if( pSrch != NULL)	
				{
					TouchKeyFound = FALSE;
					continue;
				}
			}

			pSrch = strstr( fgetData, DisplayRegKey);

			if( pSrch != NULL)	// if it is Registry Key
			{
				fprintf( hFFX1, "%s",fgetData);
				MainKeyFound = TRUE;
				MainKeyExist = TRUE;
				continue;
			}

			if(fgetData[0] == '[')	// if start Another Key, stop
			{
				if((SubKeyFound != TRUE) && MainKeyExist)
				{
					fprintf( hFFX1, "\"%s\"=dword:%x\n",sRotate,degree);
					SubKeyFound = TRUE;
				}

				MainKeyFound = FALSE;
			}

			if(MainKeyFound)
			{
				// Find "Rotate"
				pSrch = strstr( fgetData, sRotate);
				if( pSrch != NULL)	
				{
					fprintf( hFFX1, "\"%s\"=dword:%x\n",sRotate,degree);
					SubKeyFound = TRUE;
					continue;
				}
			}

			//dwBytesWritten = fwrite( fgetData, sizeof( char ), TempLen, hFFX1 );					
			fprintf( hFFX1, "%s",fgetData);


#ifdef DEV_STATUS
			atow(fgetData,TempNameW,TempLen);
			RETAILMSG(1,(L"%s\r\n", TempNameW));
#endif

		}
	}

	if( !MainKeyExist)	// if key doesn't exist, create keys
	{
		fprintf( hFFX1, "\n%s\n",DisplayRegKey);
		fprintf( hFFX1, "\"%s\"=dword:%x\n",sRotate,degree);
	}

	fclose(hFFX);
	fclose(hFFX1);

	DEBUGMSG(1,(L" UpdateDisplayRegistry : End \r\n"));

	return TRUE;
}
