
// Sample CESetup DLL

#include <windows.h>
#include <tchar.h>
#include "ce_setup.h"
#include "resource.h"

#include "gx.h"


const TCHAR szTITLE[]       = TEXT("CESetup Sample DLL");
const TCHAR szINST_INIT[]   = TEXT("Install_Init\n\nContinue?");
const TCHAR szINST_EXIT[]   = TEXT("Install_Exit\n\nContinue?");
const TCHAR szUNINST_INIT[] = TEXT("Uninstall_Init\n\nContinue?");
const TCHAR szUNINST_EXIT[] = TEXT("Uninstall_Exit");
HMODULE hDLL;
LPTSTR dir;
BOOL WINAPI DllMain(HANDLE hMod, DWORD dwReason, LPVOID lpvReserved)
{
	hDLL=hMod;
	return TRUE;
}

codeINSTALL_INIT Install_Init(
  HWND    hwndParent,
  BOOL    fFirstCall,
  BOOL    fPreviouslyInstalled,
  LPCTSTR pszInstallDir)
{
    return codeINSTALL_INIT_CONTINUE;
}


/*
HKCR,.avi,,0x00000000,avifile   
HKCR,avifile,,0x00000000,avifile
HKCR,avifile\DefaultIcon,,0x00000000,"%InstallDir%\PocketMVP.exe,-101"
HKCR,avifile\Shell\Open\Command,,0x00000000,"%InstallDir%\PocketMVP.exe" %%1
HKCR,.pdl,,0x00000000,avifile   
*/
void SaveOptions(int avi, int mp3, int pdl, int mpg, int ogg)
{
	HKEY hKey = 0;
	int ns;
	TCHAR InstallPath[300];
	TCHAR IconPath[300];
	DWORD dwDisp = 0;
	wcscpy(InstallPath,_T(""));
	wcscpy(IconPath,_T(""));
	if (wcsstr(dir,_T(" "))) wcscat(InstallPath,_T("\""));
	wcscat(InstallPath,dir);
	wcscat(InstallPath,_T("\\PocketMVP.exe"));
	if (wcsstr(dir,_T(" "))) wcscat(InstallPath,_T("\""));
	
	wcscat(InstallPath,_T(" %1\0"));
	
	//if (wcsstr(dir,_T(" "))) wcscat(IconPath,_T("\""));
	wcscat(IconPath,dir);
	wcscat(IconPath,_T("\\PocketMVP.exe"));
	
	//if (wcsstr(dir,_T(" "))) wcscat(IconPath,_T("\""));
	
	wcscat(IconPath,_T(", -101\0"));
	if ( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CLASSES_ROOT, _T(".pds"), NULL, 0, 0,
		KEY_READ | KEY_WRITE | KEY_SET_VALUE,
		NULL, &hKey, &dwDisp ) )
	{
		RegSetValueEx( hKey, NULL, 0, REG_SZ, ( LPBYTE )&(_T("txtfile")), sizeof(_T("txtfile")  ) );
		RegCloseKey( hKey );
	}

	if (avi)
	{
		if ( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CLASSES_ROOT, _T(".avi"), NULL, 0, 0,
			KEY_READ | KEY_WRITE | KEY_SET_VALUE,
			NULL, &hKey, &dwDisp ) )
		{
			RegSetValueEx( hKey, NULL, 0, REG_SZ, ( LPBYTE )&(_T("avifile")), sizeof(_T("avifile")  ) );
			RegCloseKey( hKey );
		}
		if ( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CLASSES_ROOT, _T(".divx"), NULL, 0, 0,
			KEY_READ | KEY_WRITE | KEY_SET_VALUE,
			NULL, &hKey, &dwDisp ) )
		{
			RegSetValueEx( hKey, NULL, 0, REG_SZ, ( LPBYTE )&(_T("avifile")), sizeof(_T("avifile")  ) );
			RegCloseKey( hKey );
		}
	}
	if (pdl)
	{
		if ( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CLASSES_ROOT, _T(".pdl"), NULL, 0, 0,
			KEY_READ | KEY_WRITE | KEY_SET_VALUE,
			NULL, &hKey, &dwDisp ) )
		{
			RegSetValueEx( hKey, NULL, 0, REG_SZ, ( LPBYTE )&(_T("avifile")), sizeof(_T("avifile")  ) );
			RegCloseKey( hKey );
		}
	}

	if (mpg)
	{
		if ( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CLASSES_ROOT, _T(".mpg"), NULL, 0, 0,
			KEY_READ | KEY_WRITE | KEY_SET_VALUE,
			NULL, &hKey, &dwDisp ) )
		{
			RegSetValueEx( hKey, NULL, 0, REG_SZ, ( LPBYTE )&(_T("avifile")), sizeof(_T("avifile")  ) );
			RegCloseKey( hKey );
		}
		if ( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CLASSES_ROOT, _T(".mpeg"), NULL, 0, 0,
			KEY_READ | KEY_WRITE | KEY_SET_VALUE,
			NULL, &hKey, &dwDisp ) )
		{
			RegSetValueEx( hKey, NULL, 0, REG_SZ, ( LPBYTE )&(_T("avifile")), sizeof(_T("avifile")  ) );
			RegCloseKey( hKey );
		}
	}
	if (mp3)
	{
		if ( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CLASSES_ROOT, _T(".mp3"), NULL, 0, 0,
			KEY_READ | KEY_WRITE | KEY_SET_VALUE,
			NULL, &hKey, &dwDisp ) )
		{
			RegSetValueEx( hKey, NULL, 0, REG_SZ, ( LPBYTE )&(_T("avifile")), sizeof(_T("avifile")  ) );
			RegCloseKey( hKey );
		}

	}	
	if (ogg)
	{
		if ( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CLASSES_ROOT, _T(".ogg"), NULL, 0, 0,
			KEY_READ | KEY_WRITE | KEY_SET_VALUE,
			NULL, &hKey, &dwDisp ) )
		{
			RegSetValueEx( hKey, NULL, 0, REG_SZ, ( LPBYTE )&(_T("avifile")), sizeof(_T("avifile")  ) );
			RegCloseKey( hKey );
		}
		if ( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CLASSES_ROOT, _T(".ogm"), NULL, 0, 0,
			KEY_READ | KEY_WRITE | KEY_SET_VALUE,
			NULL, &hKey, &dwDisp ) )
		{
			RegSetValueEx( hKey, NULL, 0, REG_SZ, ( LPBYTE )&(_T("avifile")), sizeof(_T("avifile")  ) );
			RegCloseKey( hKey );
		}

	}	

	if ( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CLASSES_ROOT, _T("avifile"), NULL, 0, 0,
		KEY_READ | KEY_WRITE | KEY_SET_VALUE,
		NULL, &hKey, &dwDisp ) )
	{
		RegSetValueEx( hKey, NULL, 0, REG_SZ, ( LPBYTE )&(_T("avifile")), sizeof(_T("avifile")  ) );
		RegCloseKey( hKey );
	}
	if ( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CLASSES_ROOT, _T("avifile\\DefaultIcon"), NULL, 0, 0,
		KEY_READ | KEY_WRITE | KEY_SET_VALUE,
		NULL, &hKey, &dwDisp ) )
	{
		RegSetValueEx( hKey, NULL, 0, REG_SZ, IconPath, (wcslen(IconPath)+1)*sizeof(TCHAR) );
		RegCloseKey( hKey );
	}
	if ( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CLASSES_ROOT, _T("avifile\\Shell"), NULL, 0, 0,
		KEY_READ | KEY_WRITE | KEY_SET_VALUE,
		NULL, &hKey, &dwDisp ) )
	{
		HKEY k2;
		if ( ERROR_SUCCESS == RegCreateKeyEx( hKey, _T("Open\\Command"), NULL, 0, 0,
			KEY_READ | KEY_WRITE | KEY_SET_VALUE,
			NULL, &hKey, &dwDisp ) )
		{
			RegSetValueEx( hKey, NULL, 0, REG_SZ, InstallPath, (wcslen(InstallPath)+1)*sizeof(TCHAR));
			RegCloseKey( k2 );
		}
		RegCloseKey( hKey );
	}
}

BOOL CALLBACK SetupDlg(
HWND hwndDlg, 
UINT uMsg, 
WPARAM wParam, 
LPARAM lParam )
{
	NMHDR* nm=(NMHDR*) lParam;
	int ns;
	switch(uMsg)
	{
	case WM_COMMAND:
		switch (wParam)
		{
			case IDOK:
				SaveOptions(SendMessage(GetDlgItem(hwndDlg,IDC_CHECKAVI),BM_GETCHECK,0,0)==BST_CHECKED,SendMessage(GetDlgItem(hwndDlg,IDC_CHECKMP3),BM_GETCHECK,0,0)==BST_CHECKED,SendMessage(GetDlgItem(hwndDlg,IDC_CHECKPDL),BM_GETCHECK,0,0)==BST_CHECKED,SendMessage(GetDlgItem(hwndDlg,IDC_CHECKMPG),BM_GETCHECK,0,0)==BST_CHECKED,SendMessage(GetDlgItem(hwndDlg,IDC_CHECKOGG),BM_GETCHECK,0,0)==BST_CHECKED);
				EndDialog(hwndDlg,1);
				break;
			case IDCANCEL:
				EndDialog(hwndDlg,0);
				break;
			default:
				break;

		}
		break;
		case WM_INITDIALOG:
			break;
		default:
			break;
	}
	return 0;
}

void CheckSpecial(HWND hWnd)
{

	char temp[201];
	TCHAR src[200];
	TCHAR dst[200];
#ifdef GAPI
	HANDLE hGAPI=LoadLibrary("\\Windows\\gx.dll");
	if (hGAPI) 
	{
		wcscpy(src,dir);
		wcscat(src,_T("\\gx.dll"));
		DeleteFile(src);
		FreeLibrary(hGAPI);
	}
#endif
	SystemParametersInfo(SPI_GETOEMINFO,200,(void*)temp,0);

#ifdef ARM
#ifdef GAPI
#ifndef HPC

			
			
	GXOpenDisplay(hWnd, GX_FULLSCREEN);
	GXDisplayProperties x=GXGetDisplayProperties();

//	if (wcsstr((LPTSTR)temp,_T("iPAQ"))||(wcsstr((LPTSTR)temp,_T("P6C"))))
	if (x.cbxPitch>2||(x.cbxPitch<2))
	{
		wcscpy(src,dir);
		wcscpy(dst,dir);
		wcscat(dst,_T("\\PocketMVP.exe"));
		wcscat(src,_T("\\PocketMVP_IPAQ.exe"));
		CopyFile(src,dst,FALSE);
	}
	GXCloseDisplay();
#endif
#endif
	if (wcsstr((LPTSTR)temp,_T("740"))&&(MessageBox(hWnd,_T("Would you like to install the E740 ATI optimizations?"),_T("E740 detected"),MB_YESNO)==IDYES))
	{
		wcscpy(src,dir);
		wcscpy(dst,dir);
		wcscat(dst,_T("\\PocketMVP.exe"));
		wcscat(src,_T("\\PocketMVP_ATI.exe"));
		CopyFile(src,dst,FALSE);
	}
	else
	{
		wcscpy(src,dir);
		wcscat(src,_T("\\corelinklib.dll"));
		DeleteFile(src);
	}	
	wcscpy(src,dir);
	wcscat(src,_T("\\PocketMVP_IPAQ.exe"));
	DeleteFile(src);
	wcscpy(src,dir);
	wcscat(src,_T("\\PocketMVP_ATI.exe"));
	DeleteFile(src);
#endif

}

codeINSTALL_EXIT Install_Exit(
  HWND    hwndParent,
  LPCTSTR pszInstallDir,
  WORD    cFailedDirs,
  WORD    cFailedFiles,
  WORD    cFailedRegKeys,
  WORD    cFailedRegVals,
  WORD    cFailedShortcuts)
{
	dir=pszInstallDir;
	CheckSpecial(hwndParent);
  DialogBox(hDLL,MAKEINTRESOURCE(IDD_DIALOG1),hwndParent,SetupDlg);
  return codeINSTALL_EXIT_DONE;
}

codeUNINSTALL_INIT Uninstall_Init(HWND hwndParent, LPCTSTR pszInstallDir)
{
    return codeUNINSTALL_INIT_CONTINUE;
}

codeUNINSTALL_EXIT Uninstall_Exit(HWND hwndParent)
{
  return codeUNINSTALL_EXIT_DONE;
}
