// Castce.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Castce.h"
#include <commctrl.h>
#include <aygshell.h>
#include <commdlg.h>
#include <sipapi.h>
#include <windowsx.h>
#include <gx.h>
#include "VOIMAGE.H"
#include "config.h"

#define MAX_LOADSTRING 100
#define MAXFILENAMELENGHT 1024




extern "C" char fname[1024];
extern "C" unsigned char key_joy_up,key_joy_down,key_joy_left,key_joy_right,key_joy_fire,key_mouse_left,key_mouse_right,key_mouse_calib,key_disk_change,joy_on_stylus,joy_sensibility;
extern "C" void SetMemW(unsigned long address, unsigned short value);
extern "C" int savestate(int numstate,unsigned short *rootapp,HWND hWnd);
extern "C" int loadstate(int numstate,unsigned short *rootapp,HWND hWnd);
extern "C" int speedbar,nbframeskip,autoframeskip,disass,autofirespeed;
extern "C" int checkedsample;
extern "C" int checkedsound;
extern "C" unsigned char joynum,compat;
extern "C" int main(int mode,HINSTANCE			g_hInst);
extern "C" int FDCInit(int i);
extern "C" int FDCchange(int ret);
extern "C" int FDCeject(int i);
extern "C" HWND curhWnd;
struct Disk {
    FILE       *file;
    char        name[80];
    int         head;
    int         sides;
    int         tracks;
    int         sectors;
    int         secsize;
};
extern "C" struct Disk disk[2];
extern "C" int gapiypitch,gapixpitch;
GXDisplayProperties g_gxdp;

// Global Variables:
HWND g_hMainDlg;
HINSTANCE			g_hInst;				// The current instance
HWND				g_hwndCB;					// The command bar handle
CVOImage			g_pngLogo,g_pngGreat;
HDC                 g_hLogoDC,g_hGreatDC;
static TCHAR		g_sRootKey[256];
int   nSelectedPatch;
int ret,choice;
int whichpng=0;
int sides,tracks,sectors;
int ptsides,pttracks,ptsectors,ptautofire,ptsensibility;
static SHACTIVATEINFO s_sai;
TCHAR ApplFilepath[MAX_PATH];
TCHAR			TOSFileName[MAXFILENAMELENGHT] = TEXT("\0");
extern "C" unsigned char PathName[400];
struct Patchs {
    char	name[80];
    char	comment[80];
	int realtime;
    unsigned long	addr[20];
	unsigned short	word[20];
};
extern "C" struct Patchs Patch[50];
extern "C" int PatchSelected;
extern "C" int RealTimePatch;
char PatchFile[4096*2]; 
int reseted=0;						// template

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass	(HINSTANCE, LPTSTR);
BOOL				InitInstance	(HINSTANCE, int);
LRESULT CALLBACK	WndProc			(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	Fdc			(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	ApplyingPatch		(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	Autofirespeed		(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	Keycfg		(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	JoySensibility		(HWND, UINT, WPARAM, LPARAM);
HWND				CreateRpCommandBar(HWND);
bool                LoadOptions();
void                SaveOptions();
void				ReadTosVer();

void ConvertTToC(CHAR* pszDest, const TCHAR* pszSrc)
{
	for(int i = 0; i < _tcslen(pszSrc); i++)
		pszDest[i] = (CHAR) pszSrc[i];
}

void ConvertCToT(TCHAR* pszDest, const CHAR* pszSrc)
{
	for(int i = 0; i < strlen(pszSrc); i++)
		pszDest[i] = (TCHAR) pszSrc[i];
}


//------------------------------------------------------------------------------
// OpenRegistry
//      Opens and returns a key to the program's registry.
//------------------------------------------------------------------------------
HKEY OpenRegistry()
{
	HKEY  hRootKey;
    DWORD dwDisposition;
	
	if (ERROR_SUCCESS == RegCreateKeyEx(HKEY_LOCAL_MACHINE, g_sRootKey, 0, NULL, 0, 0, NULL, &hRootKey, &dwDisposition))
	{
		return hRootKey;
	}
	
	return NULL;
}

//------------------------------------------------------------------------------
// CloseRegistry
//      Closes the program's registry.
//------------------------------------------------------------------------------
void CloseRegistry(HKEY hRootKey)
{
	RegCloseKey(hRootKey);
}

//------------------------------------------------------------------------------
// RegQueryBool
//      Reads a boolean flag from the registry.
//------------------------------------------------------------------------------
bool RegQueryBool(HKEY hKey, LPCWSTR lpValueName, bool bDefault = true)
{
    DWORD dwValue;
    DWORD dwType;
	DWORD dwSize   = sizeof(dwValue);
	
	if (ERROR_SUCCESS == RegQueryValueEx(hKey, lpValueName, 0, &dwType, (unsigned char *) &dwValue, &dwSize))
	{
        return ((DWORD) 0 != dwValue);
    }
	
    return bDefault;
}

//------------------------------------------------------------------------------
// RegSetBool
//      Sets a boolean flag in the registry.
//------------------------------------------------------------------------------
void RegSetBool(HKEY hKey, LPCWSTR lpValueName, bool bValue)
{
	DWORD dwValue  = (DWORD) bValue;
	
	RegSetValueEx(hKey, lpValueName, 0, REG_DWORD, (unsigned char *) &dwValue, sizeof(dwValue));
}

//------------------------------------------------------------------------------
// RegQueryDword
//      Reads a dword value from the registry.
//------------------------------------------------------------------------------
DWORD RegQueryDword(HKEY hKey, LPCWSTR lpValueName, DWORD dwDefault)
{
    DWORD dwValue;
    DWORD dwType;
	DWORD dwSize   = sizeof(dwValue);
	
	if (ERROR_SUCCESS == RegQueryValueEx(hKey, lpValueName, 0, &dwType, (unsigned char *) &dwValue, &dwSize))
	{
        return dwValue;
    }
	
    return dwDefault;
}

//------------------------------------------------------------------------------
// RegSetDword
//      Sets a dword value in the registry.
//------------------------------------------------------------------------------
void RegSetDword(HKEY hKey, LPCWSTR lpValueName, DWORD dwValue)
{
	RegSetValueEx(hKey, lpValueName, 0, REG_DWORD, (unsigned char *) &dwValue, sizeof(dwValue));
}


WCHAR *RegQueryString(HKEY hKey, WCHAR *lpValueName,WCHAR *dwValue, WCHAR *dwDefault)
{
    DWORD dwType;
	DWORD dwSize   = 1024;
	
	if (ERROR_SUCCESS == RegQueryValueEx(hKey, lpValueName, NULL, NULL , (LPBYTE)dwValue, &dwSize))
	{
        return dwValue;
    }
	
    return dwDefault;
}

//------------------------------------------------------------------------------
// RegSetDword
//      Sets a dword value in the registry.
//------------------------------------------------------------------------------
void RegSetString(HKEY hKey, WCHAR *lpValueName, WCHAR *dwValue)
{
	RegSetValueEx(hKey, lpValueName, 0, REG_SZ, (BYTE*)dwValue, (wcslen(dwValue) + 1) * sizeof(WCHAR));
}



int WINAPI WinMain(	HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPTSTR    lpCmdLine,
				   int       nCmdShow)
{
	MSG msg;
	HACCEL hAccelTable;
	
	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}
	
	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_CASTCE);
	
	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	
    SaveOptions();
	return msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    It is important to call this function so that the application 
//    will get 'well formed' small icons associated with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
	WNDCLASS	wc;
	
    wc.style			= CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc		= (WNDPROC) WndProc;
    wc.cbClsExtra		= 0;
    wc.cbWndExtra		= 0;
    wc.hInstance		= hInstance;
    wc.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CASTCE));
    wc.hCursor			= 0;
    wc.hbrBackground	= (HBRUSH) GetStockObject(BLACK_BRUSH);
    wc.lpszMenuName		= 0;
    wc.lpszClassName	= szWindowClass;
	
	return RegisterClass(&wc);
}

//
//  FUNCTION: InitInstance(HANDLE, int)
//
//  PURPOSE: Saves instance handle and creates main window
//
//  COMMENTS:
//
//    In this function, we save the instance handle in a global variable and
//    create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND	hWnd = NULL;
	TCHAR	szTitle[MAX_LOADSTRING];			// The title bar text
	TCHAR	szWindowClass[MAX_LOADSTRING];		// The window class name
	HMENU hMenu;
	g_hInst = hInstance;		// Store instance handle in our global variable
	// Initialize global strings
	LoadString(hInstance, IDC_CASTCE, szWindowClass, MAX_LOADSTRING);
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	
	//If it is already running, then focus on the window
	hWnd = FindWindow(szWindowClass, szTitle);	
	if (hWnd) 
	{
		// set focus to foremost child window
		// The "| 0x01" is used to bring any owned windows to the foreground and
		// activate them.
		SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
		return 0;
	} 
	
	MyRegisterClass(hInstance, szWindowClass);
	
	RECT	rect;
	GetClientRect(hWnd, &rect);
	
	hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
	if (!hWnd)
	{	
		return FALSE;
	}
	//When the main window is created using CW_USEDEFAULT the height of the menubar (if one
	// is created is not taken into account). So we resize the window after creating it
	// if a menubar is present
	{
		RECT rc;
		GetWindowRect(hWnd, &rc);
		rc.bottom -= MENU_HEIGHT;
		if (g_hwndCB)
			MoveWindow(hWnd, rc.left, rc.top, rc.right, rc.bottom, FALSE);
	}
	HDC		hDC = GetDC(hWnd);
	
	g_hLogoDC = CreateCompatibleDC(hDC);
	g_pngLogo.SetBitmap(g_hLogoDC, IDR_INTRO, TEXT("IMAGE"), GetModuleHandle(NULL));
	SelectObject(g_hLogoDC, (HBITMAP)g_pngLogo);
	
    _tcscpy(g_sRootKey, _T("SOFTWARE\\Apps\\CastCE"));
    LoadOptions();
#ifndef DISASS
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	RemoveMenu(hMenu, ID_DISASS, MF_BYCOMMAND);
#endif
	ReleaseDC(hWnd, hDC);
	
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
	
	return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	
	TCHAR			ISOFileNameA[MAXFILENAMELENGHT] = TEXT("\0");
	TCHAR			ISOFileNameB[MAXFILENAMELENGHT] = TEXT("\0");
	
	TCHAR			SaveFileNameA[MAXFILENAMELENGHT] = TEXT("\0");
	TCHAR			RootPath[MAXFILENAMELENGHT] = TEXT("\0");
	OPENFILENAME	ofntos;
	OPENFILENAME	ofnb;
	OPENFILENAME	ofna;
	OPENFILENAME	ofns;
	OPENFILENAME	ofno;
	
	HDC hdc,hDC;
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	TCHAR szHello[MAX_LOADSTRING];
	TCHAR szDisk1[MAX_LOADSTRING];
	TCHAR szDisk2[MAX_LOADSTRING];
	curhWnd=hWnd;
	HWND hWndTB;
	RECT rc;
	HMENU hMenu;
	HKEY  hRootKey;
	
	unsigned int i,j;
	long lastic;
	GetModuleFileName(0, ApplFilepath, MAX_PATH);
	
	
	
	
	
	
	
	switch (message) 
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam); 
		wmEvent = HIWORD(wParam); 
		// Parse the menu selections:
		switch (wmId)
		{	
		case IDM_MAIN_COMMAND2:
			if (MessageBox(hWnd,L"Are you sure to quit?",L"Info",MB_OKCANCEL)==1) DestroyWindow(hWnd);
			break;
		case ID_CPU_RESET:
			ReadTosVer();
			if (strlen((const char *)PathName)==0){
				MessageBox(hWnd,L"Load TOS-rom first",L"Info",MB_OK);
				break;
			}
			
			GetWindowRect(hWnd, &rc);
			CommandBar_Show(g_hwndCB,FALSE);
			hWndTB=FindWindow(TEXT("HHTaskbar"),NULL);
			SHFullScreen( hWnd, SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON + SHFS_HIDESTARTICON );
			ShowWindow( hWndTB, SW_HIDE );
			MoveWindow( hWnd, rc.left,rc.top - 26,rc.right,rc.bottom + 26,TRUE );
			GXOpenDisplay( NULL, GX_FULLSCREEN );
			g_gxdp=GXGetDisplayProperties();
			gapixpitch=g_gxdp.cbxPitch;
			gapiypitch=g_gxdp.cbyPitch;
			GXOpenInput();
			main(0,g_hInst);
			GXCloseInput();
			GXCloseDisplay();
			GetWindowRect(hWnd, &rc);
			hWndTB=FindWindow(TEXT("HHTaskbar"),NULL);
			SHFullScreen( hWnd, SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON + SHFS_SHOWSTARTICON );
			CommandBar_Show(g_hwndCB,TRUE);
			ShowWindow( hWndTB, SW_SHOW );
			MoveWindow( hWnd, rc.left, 	rc.top + 26,rc.right, rc.bottom - 2 * 26,TRUE );
			CommandBar_Show(g_hwndCB,TRUE);
			reseted=1;
			break;
			
		case ID_DISK_LOADA:
			
			
			memset(&ofna, 0, sizeof(ofna));
			ofna.lStructSize   = sizeof(ofna);
			ofna.hwndOwner = hWnd;
			ofna.lpstrFile = ISOFileNameA;
			ofna.nMaxFile = MAXFILENAMELENGHT;   
			ofna.lpstrFilter = TEXT("Disk images (*.st;*.msa;*.zip)\0*.st;*.msa;*.zip\0");   
			ofna.lpstrTitle = TEXT("Select DiskA");
			ofna.Flags = OFN_EXPLORER;
			
			memset (&ISOFileNameA[0], 0, sizeof (ISOFileNameA));
			if (GetOpenFileName(&ofna)){
				i=0;
				while ((char )(ISOFileNameA[i])!=0){
					fname[i]=(char )(ISOFileNameA[i]);
					i++;
					
				}
				fname[i]='\0';
				strncpy (disk[0].name, fname, 80);
				ret=FDCInit(0);
			}else ret=0;
			if (ret) DialogBox(g_hInst, (LPCTSTR)IDD_FDC, hWnd, (DLGPROC)Fdc);
			break;
			
		case ID_DISK_LOADB:
			memset(&ofnb, 0, sizeof(ofnb));
			ofnb.lStructSize   = sizeof(ofnb);
			ofnb.hwndOwner = hWnd;
			ofnb.lpstrFile = ISOFileNameB;
			ofnb.nMaxFile = MAXFILENAMELENGHT;   
			ofnb.lpstrFilter = TEXT("Disk images (*.st;*.msa;*.zip)\0*.st;*.msa;*.zip\0");   
			ofnb.lpstrTitle = TEXT("Select DiskB");
			ofnb.Flags = OFN_EXPLORER;
			memset (&ISOFileNameB[0], 0, sizeof (ISOFileNameB));
			if (GetOpenFileName(&ofnb)){
				i=0;
				while ((char )(ISOFileNameB[i])!=0){
					fname[i]=(char )(ISOFileNameB[i]);
					i++;
					
				}
				fname[i]='\0';
				strncpy (disk[1].name, fname, 80);
				ret=FDCInit(1);
			}else ret=0;
			if (ret) DialogBox(g_hInst, (LPCTSTR)IDD_FDC, hWnd, (DLGPROC)Fdc);
			break;
			
		case ID_CPU_CONTINUE:
			if (!reseted){
				MessageBox(hWnd,L"You must Reset before!!!",L"Info",MB_OK);
				break;
			}
			GetWindowRect(hWnd, &rc);
			CommandBar_Show(g_hwndCB,FALSE);
			hWndTB=FindWindow(TEXT("HHTaskbar"),NULL);
			SHFullScreen( hWnd, SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON + SHFS_HIDESTARTICON );
			ShowWindow( hWndTB, SW_HIDE );
			MoveWindow( hWnd, rc.left,rc.top - 26,rc.right,rc.bottom + 26,TRUE );
			GXOpenDisplay( NULL, GX_FULLSCREEN );
			g_gxdp=GXGetDisplayProperties();
			gapixpitch=g_gxdp.cbxPitch;
			gapiypitch=g_gxdp.cbyPitch;
			GXOpenInput();
			main(3,g_hInst);
			GXCloseInput();
			GXCloseDisplay();
			GetWindowRect(hWnd, &rc);
			hWndTB=FindWindow(TEXT("HHTaskbar"),NULL);
			SHFullScreen( hWnd, SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON + SHFS_SHOWSTARTICON );
			CommandBar_Show(g_hwndCB,TRUE);
			ShowWindow( hWndTB, SW_SHOW );
			MoveWindow( hWnd, rc.left, 	rc.top + 26,rc.right, rc.bottom - 2 * 26,TRUE );
			CommandBar_Show(g_hwndCB,TRUE);
			break;	
		case ID_DISK_EJECTA:
			FDCeject(0);
			MessageBox(hWnd,L"Disk A ejected",L"Info",MB_OK);
			break;
		case ID_DISK_EJECTB:
			FDCeject(1);
			MessageBox(hWnd,L"Disk B ejected",L"Info",MB_OK);
			break;
		case ID_PATCH:
			DialogBox(g_hInst, (LPCTSTR)IDD_PATCH, hWnd, (DLGPROC)ApplyingPatch);
			break;
		case ID_AUTOFIRESPEED:
			DialogBox(g_hInst, (LPCTSTR)IDD_AUTOFIRESPEED, hWnd, (DLGPROC)Autofirespeed);
			break;
		case ID_JOYSENSIBILITY:
			DialogBox(g_hInst, (LPCTSTR)IDD_SENSIBILITY, hWnd, (DLGPROC)JoySensibility);
			break;
		case ID_JOY0:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_JOY0, MF_BYCOMMAND | MF_CHECKED);
			CheckMenuItem (hMenu, ID_JOY1, MF_BYCOMMAND | MF_UNCHECKED);
			joynum=0xfe;
			SaveOptions();
			break;
		case ID_JOY1:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_JOY1, MF_BYCOMMAND | MF_CHECKED);
			CheckMenuItem (hMenu, ID_JOY0, MF_BYCOMMAND | MF_UNCHECKED);
			joynum=0xff;
			SaveOptions();
			break;		
		case ID_JOYSTYLUS:
			if (!joy_on_stylus){
				hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
				CheckMenuItem (hMenu, ID_JOYSTYLUS, MF_BYCOMMAND | MF_CHECKED);
				joy_on_stylus=1;
			}else{
				hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
				CheckMenuItem (hMenu, ID_JOYSTYLUS, MF_BYCOMMAND | MF_UNCHECKED);
				joy_on_stylus=0;
			}	
			break;
		case IDOK:
			SendMessage(hWnd, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), (LPARAM)hWnd);
			SendMessage (hWnd, WM_CLOSE, 0, 0);
			break;
		case IDM_ABOUT:
			hDC = GetDC(hWnd);
			whichpng=~whichpng;
			if (whichpng==0){
				DeleteObject(g_hGreatDC);
				g_hLogoDC = CreateCompatibleDC(hDC);
				g_pngLogo.SetBitmap(g_hLogoDC, IDR_INTRO, TEXT("IMAGE"), GetModuleHandle(NULL));
				SelectObject(g_hLogoDC, (HBITMAP)g_pngLogo);
			}else{
				DeleteObject(g_hLogoDC);
				g_hGreatDC = CreateCompatibleDC(hDC);
				g_pngGreat.SetBitmap(g_hGreatDC, IDR_GREAT, TEXT("IMAGE"), GetModuleHandle(NULL));
				SelectObject(g_hGreatDC, (HBITMAP)g_pngGreat);
			}
			ReleaseDC(hWnd, hDC);
			GetWindowRect(hWnd, &rc);
			rc.top    = 0;
			rc.bottom = 320 - 26;
			SetWindowPos(hWnd, HWND_TOP, rc.left, rc.top, rc.right, rc.bottom, 0);
			rc.top    = 26;
			rc.bottom = 320 - 26;
			MoveWindow( hWnd, rc.left, 	rc.top ,rc.right, rc.bottom,TRUE );
			break;
		case ID_SOUND_ON:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_SOUND_ON, MF_BYCOMMAND | MF_CHECKED);
			CheckMenuItem (hMenu, ID_SOUND_OFF, MF_BYCOMMAND | MF_UNCHECKED);
			checkedsound=1;
			SaveOptions();
			break;
		case ID_SOUND_OFF:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_SOUND_ON, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_SOUND_OFF, MF_BYCOMMAND | MF_CHECKED);
			CheckMenuItem (hMenu, ID_SOUND_SAMPLE_ON, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_SOUND_SAMPLE_OFF, MF_BYCOMMAND | MF_CHECKED);
			checkedsound=0;
			checkedsample=0;
			SaveOptions();
			break;
		case ID_SOUND_SAMPLE_ON:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_SOUND_OFF, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_SOUND_ON, MF_BYCOMMAND | MF_CHECKED);
			CheckMenuItem (hMenu, ID_SOUND_SAMPLE_OFF, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_SOUND_SAMPLE_ON, MF_BYCOMMAND | MF_CHECKED);
			checkedsound=1;
			checkedsample=1;
			SaveOptions();
			break;
		case ID_SOUND_SAMPLE_OFF:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_SOUND_SAMPLE_ON, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_SOUND_SAMPLE_OFF, MF_BYCOMMAND | MF_CHECKED);
			checkedsample=0;
			SaveOptions();
			break;
		case ID_SPEEDBAR_ON:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_SPEEDBAR_OFF, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_SPEEDBAR_ON, MF_BYCOMMAND | MF_CHECKED);
			speedbar=1;
			SaveOptions();
			break;
		case ID_SPEEDBAR_OFF:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_SPEEDBAR_ON, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_SPEEDBAR_OFF, MF_BYCOMMAND | MF_CHECKED);
			speedbar=0;
			SaveOptions();
			break;
		case ID_FRAMESKIP_NONE:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_FRAMESKIP_1, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_2, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_3, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_4, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_NONE, MF_BYCOMMAND | MF_CHECKED);
			nbframeskip=0;
			SaveOptions();
			break;
		case ID_FRAMESKIP_1:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_FRAMESKIP_NONE, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_2, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_3, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_4, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_1, MF_BYCOMMAND | MF_CHECKED);
			nbframeskip=1;
			SaveOptions();
			break;
		case ID_FRAMESKIP_2:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_FRAMESKIP_NONE, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_1, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_3, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_4, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_2, MF_BYCOMMAND | MF_CHECKED);
			nbframeskip=2;
			SaveOptions();
			break;
		case ID_FRAMESKIP_3:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_FRAMESKIP_NONE, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_1, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_2, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_3, MF_BYCOMMAND | MF_CHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_4, MF_BYCOMMAND | MF_UNCHECKED);
			nbframeskip=3;
			SaveOptions();
			break;
		case ID_FRAMESKIP_4:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_FRAMESKIP_NONE, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_1, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_2, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_3, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_FRAMESKIP_4, MF_BYCOMMAND | MF_CHECKED);
			nbframeskip=4;
			SaveOptions();
			break;
		case ID_AUTO_FRAMESKIP:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			if (autoframeskip==0){
				autoframeskip=1;
				CheckMenuItem (hMenu, ID_AUTO_FRAMESKIP, MF_BYCOMMAND | MF_CHECKED);
			}else{
				autoframeskip=0;
				CheckMenuItem (hMenu, ID_AUTO_FRAMESKIP, MF_BYCOMMAND | MF_UNCHECKED);
			}
			SaveOptions();
			break;
		case ID_COMPAT0:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_COMPAT0, MF_BYCOMMAND | MF_CHECKED);
			CheckMenuItem (hMenu, ID_COMPAT1, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_COMPAT2, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_COMPAT3, MF_BYCOMMAND | MF_UNCHECKED);
			compat=0;
			SaveOptions();
			break;
		case ID_COMPAT1:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_COMPAT0, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_COMPAT1, MF_BYCOMMAND | MF_CHECKED);
			CheckMenuItem (hMenu, ID_COMPAT2, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_COMPAT3, MF_BYCOMMAND | MF_UNCHECKED);
			compat=1;
			SaveOptions();
			break;
		case ID_COMPAT2:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_COMPAT0, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_COMPAT1, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_COMPAT2, MF_BYCOMMAND | MF_CHECKED);
			CheckMenuItem (hMenu, ID_COMPAT3, MF_BYCOMMAND | MF_UNCHECKED);
			compat=2;
			SaveOptions();
			break;
		case ID_COMPAT3:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			CheckMenuItem (hMenu, ID_COMPAT0, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_COMPAT1, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_COMPAT2, MF_BYCOMMAND | MF_UNCHECKED);
			CheckMenuItem (hMenu, ID_COMPAT3, MF_BYCOMMAND | MF_CHECKED);
			compat=3;
			SaveOptions();
			break;
			
		case ID_DISASS:
			hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
			if (disass==0){
				disass=1;
				CheckMenuItem (hMenu, ID_DISASS, MF_BYCOMMAND | MF_CHECKED);
			}else{
				disass=0;
				CheckMenuItem (hMenu, ID_DISASS, MF_BYCOMMAND | MF_UNCHECKED);
			}
			break;
			
			
		case ID_TOS_ROM:
			memset(&ofntos, 0, sizeof(ofntos));
			ofntos.lStructSize   = sizeof(ofntos);
			ofntos.hwndOwner = hWnd;
			ofntos.lpstrFile = TOSFileName;
			ofntos.nMaxFile = MAXFILENAMELENGHT;   
			ofntos.lpstrFilter = TEXT("TOS rom (*.rom)\0*.rom\0");   
			ofntos.lpstrTitle = TEXT("Select TOS rom");
			ofntos.Flags = OFN_EXPLORER;
			memset (&TOSFileName[0], 0, sizeof (TOSFileName));
			if (GetOpenFileName(&ofntos)){
				i=0;
				while ((char )(TOSFileName[i])!=0){
					PathName[i]=(char )(TOSFileName[i]);
					i++;
				}
				PathName[i]='\0';
			}
			ReadTosVer();
			SaveOptions();
			break;
		case ID_SAVE_STATE1:
			if (!reseted){
				MessageBox(hWnd,L"You must Reset before!!!",L"Info",MB_OK);
				break;
			}
			savestate(1,ApplFilepath,hWnd);
			break;
		case ID_SAVE_STATE2:
			if (!reseted){
				MessageBox(hWnd,L"You must Reset before!!!",L"Info",MB_OK);
				break;
			}
			savestate(2,ApplFilepath,hWnd);
			break;
		case ID_SAVE_STATE3:
			if (!reseted){
				MessageBox(hWnd,L"You must Reset before!!!",L"Info",MB_OK);
				break;
			}
			savestate(3,ApplFilepath,hWnd);
			break;
		case ID_SAVE_STATE4:
			if (!reseted){
				MessageBox(hWnd,L"You must Reset before!!!",L"Info",MB_OK);
				break;
			}
			savestate(4,ApplFilepath,hWnd);
			break;
		case ID_SAVE_STATE5:
			if (!reseted){
				MessageBox(hWnd,L"You must Reset before!!!",L"Info",MB_OK);
				break;
			}
			savestate(5,ApplFilepath,hWnd);
			break;
		case ID_SAVE_STATE_CHOOSE:
			if (!reseted){
				MessageBox(hWnd,L"You must Reset before!!!",L"Info",MB_OK);
				break;
			}
			memset (&RootPath[0], 0, sizeof (RootPath));
			wcscpy(RootPath,ApplFilepath);
			i=wcslen(RootPath)-1;
			while ((char )(RootPath[i])!='\\') RootPath[i--]=0;
			wcscat(RootPath,L"save\\");
			memset (&SaveFileNameA[0], 0, sizeof (SaveFileNameA));
			memset(&ofns, 0, sizeof(ofns));
			ofns.lStructSize   = sizeof(ofns);
			ofns.hwndOwner = hWnd;
			ofns.lpstrFile = SaveFileNameA;
			ofns.nMaxFile = MAXFILENAMELENGHT;   
			ofns.lpstrFilter = TEXT("Saving State (*.ust)\0*.ust\0");   
			ofns.lpstrTitle = TEXT("Saving State");
			ofns.Flags = OFN_EXPLORER;
			
			
			
			if (GetSaveFileName(&ofns)){	
				
				savestate(0,SaveFileNameA,hWnd);	
				SHSipPreference(hWnd, SIP_FORCEDOWN);
			}
			break;
		case ID_LOAD_STATE1:
			if (!loadstate(1,ApplFilepath,hWnd)){
				GetWindowRect(hWnd, &rc);
				CommandBar_Show(g_hwndCB,FALSE);
				hWndTB=FindWindow(TEXT("HHTaskbar"),NULL);
				SHFullScreen( hWnd, SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON + SHFS_HIDESTARTICON );
				ShowWindow( hWndTB, SW_HIDE );
				MoveWindow( hWnd, rc.left,rc.top - 26,rc.right,rc.bottom + 26,TRUE );
				GXOpenDisplay( NULL, GX_FULLSCREEN );
				g_gxdp=GXGetDisplayProperties();
				gapixpitch=g_gxdp.cbxPitch;
				gapiypitch=g_gxdp.cbyPitch;
				GXOpenInput();
				main(5,g_hInst);
				GXCloseInput();
				GXCloseDisplay();
				GetWindowRect(hWnd, &rc);
				hWndTB=FindWindow(TEXT("HHTaskbar"),NULL);
				SHFullScreen( hWnd, SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON + SHFS_SHOWSTARTICON );
				CommandBar_Show(g_hwndCB,TRUE);
				ShowWindow( hWndTB, SW_SHOW );
				MoveWindow( hWnd, rc.left, 	rc.top + 26,rc.right, rc.bottom - 2 * 26,TRUE );
				CommandBar_Show(g_hwndCB,TRUE);
				ReadTosVer();
				reseted=1;
				SaveOptions();
				LoadOptions();
			}
			break;
		case ID_LOAD_STATE2:
			if (!loadstate(2,ApplFilepath,hWnd)){
				GetWindowRect(hWnd, &rc);
				CommandBar_Show(g_hwndCB,FALSE);
				hWndTB=FindWindow(TEXT("HHTaskbar"),NULL);
				SHFullScreen( hWnd, SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON + SHFS_HIDESTARTICON );
				ShowWindow( hWndTB, SW_HIDE );
				MoveWindow( hWnd, rc.left,rc.top - 26,rc.right,rc.bottom + 26,TRUE );
				GXOpenDisplay( NULL, GX_FULLSCREEN );
				g_gxdp=GXGetDisplayProperties();
				gapixpitch=g_gxdp.cbxPitch;
				gapiypitch=g_gxdp.cbyPitch;
				GXOpenInput();
				main(5,g_hInst);
				GXCloseInput();
				GXCloseDisplay();
				GetWindowRect(hWnd, &rc);
				hWndTB=FindWindow(TEXT("HHTaskbar"),NULL);
				SHFullScreen( hWnd, SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON + SHFS_SHOWSTARTICON );
				CommandBar_Show(g_hwndCB,TRUE);
				ShowWindow( hWndTB, SW_SHOW );
				MoveWindow( hWnd, rc.left, 	rc.top + 26,rc.right, rc.bottom - 2 * 26,TRUE );
				CommandBar_Show(g_hwndCB,TRUE);
				ReadTosVer();
				reseted=1;
				SaveOptions();
				LoadOptions();
			}
			break;
		case ID_LOAD_STATE3:
			if (!loadstate(3,ApplFilepath,hWnd)){
				GetWindowRect(hWnd, &rc);
				CommandBar_Show(g_hwndCB,FALSE);
				hWndTB=FindWindow(TEXT("HHTaskbar"),NULL);
				SHFullScreen( hWnd, SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON + SHFS_HIDESTARTICON );
				ShowWindow( hWndTB, SW_HIDE );
				MoveWindow( hWnd, rc.left,rc.top - 26,rc.right,rc.bottom + 26,TRUE );
				GXOpenDisplay( NULL, GX_FULLSCREEN );
				g_gxdp=GXGetDisplayProperties();
				gapixpitch=g_gxdp.cbxPitch;
				gapiypitch=g_gxdp.cbyPitch;
				GXOpenInput();
				main(5,g_hInst);
				GXCloseInput();
				GXCloseDisplay();
				GetWindowRect(hWnd, &rc);
				hWndTB=FindWindow(TEXT("HHTaskbar"),NULL);
				SHFullScreen( hWnd, SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON + SHFS_SHOWSTARTICON );
				CommandBar_Show(g_hwndCB,TRUE);
				ShowWindow( hWndTB, SW_SHOW );
				MoveWindow( hWnd, rc.left, 	rc.top + 26,rc.right, rc.bottom - 2 * 26,TRUE );
				CommandBar_Show(g_hwndCB,TRUE);
				ReadTosVer();
				reseted=1;
				SaveOptions();
				LoadOptions();
			}
			break;
		case ID_LOAD_STATE4:
			if (!loadstate(4,ApplFilepath,hWnd)){
				GetWindowRect(hWnd, &rc);
				CommandBar_Show(g_hwndCB,FALSE);
				hWndTB=FindWindow(TEXT("HHTaskbar"),NULL);
				SHFullScreen( hWnd, SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON + SHFS_HIDESTARTICON );
				ShowWindow( hWndTB, SW_HIDE );
				MoveWindow( hWnd, rc.left,rc.top - 26,rc.right,rc.bottom + 26,TRUE );
				GXOpenDisplay( NULL, GX_FULLSCREEN );
				g_gxdp=GXGetDisplayProperties();
				gapixpitch=g_gxdp.cbxPitch;
				gapiypitch=g_gxdp.cbyPitch;
				GXOpenInput();
				main(5,g_hInst);
				GXCloseInput();
				GXCloseDisplay();
				GetWindowRect(hWnd, &rc);
				hWndTB=FindWindow(TEXT("HHTaskbar"),NULL);
				SHFullScreen( hWnd, SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON + SHFS_SHOWSTARTICON );
				CommandBar_Show(g_hwndCB,TRUE);
				ShowWindow( hWndTB, SW_SHOW );
				MoveWindow( hWnd, rc.left, 	rc.top + 26,rc.right, rc.bottom - 2 * 26,TRUE );
				CommandBar_Show(g_hwndCB,TRUE);
				ReadTosVer();
				reseted=1;
				SaveOptions();
				LoadOptions();
			}
			break;
		case ID_LOAD_STATE5:
			if (!loadstate(5,ApplFilepath,hWnd)){
				GetWindowRect(hWnd, &rc);
				CommandBar_Show(g_hwndCB,FALSE);
				hWndTB=FindWindow(TEXT("HHTaskbar"),NULL);
				SHFullScreen( hWnd, SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON + SHFS_HIDESTARTICON );
				ShowWindow( hWndTB, SW_HIDE );
				MoveWindow( hWnd, rc.left,rc.top - 26,rc.right,rc.bottom + 26,TRUE );
				GXOpenDisplay( NULL, GX_FULLSCREEN );
				g_gxdp=GXGetDisplayProperties();
				gapixpitch=g_gxdp.cbxPitch;
				gapiypitch=g_gxdp.cbyPitch;
				GXOpenInput();
				main(5,g_hInst);
				GXCloseInput();
				GXCloseDisplay();
				GetWindowRect(hWnd, &rc);
				hWndTB=FindWindow(TEXT("HHTaskbar"),NULL);
				SHFullScreen( hWnd, SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON + SHFS_SHOWSTARTICON );
				CommandBar_Show(g_hwndCB,TRUE);
				ShowWindow( hWndTB, SW_SHOW );
				MoveWindow( hWnd, rc.left, 	rc.top + 26,rc.right, rc.bottom - 2 * 26,TRUE );
				CommandBar_Show(g_hwndCB,TRUE);
				ReadTosVer();
				reseted=1;
				SaveOptions();
				LoadOptions();
			}
			break;
		case ID_LOAD_STATE_CHOOSE:
			memset (&RootPath[0], 0, sizeof (RootPath));
			wcscpy(RootPath,ApplFilepath);
			i=wcslen(RootPath)-1;
			while ((char )(RootPath[i])!='\\') RootPath[i--]=0;
			memset (&SaveFileNameA[0], 0, sizeof (SaveFileNameA));
			memset(&ofno, 0, sizeof(ofno));
			ofno.lStructSize   = sizeof(ofno);
			ofno.hwndOwner = hWnd;
			ofno.lpstrFile = SaveFileNameA;
			ofno.nMaxFile = MAXFILENAMELENGHT;   
			ofno.lpstrFilter = TEXT("Open SaveState (*.ust)\0*.ust\0");   
			ofno.lpstrTitle = TEXT("Select SaveState");
			ofno.Flags = OFN_EXPLORER;
			
			if (GetOpenFileName(&ofno)){			
				if (!loadstate(0,SaveFileNameA,hWnd)){
					GetWindowRect(hWnd, &rc);
					CommandBar_Show(g_hwndCB,FALSE);
					hWndTB=FindWindow(TEXT("HHTaskbar"),NULL);
					SHFullScreen( hWnd, SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON + SHFS_HIDESTARTICON );
					ShowWindow( hWndTB, SW_HIDE );
					MoveWindow( hWnd, rc.left,rc.top - 26,rc.right,rc.bottom + 26,TRUE );
					GXOpenDisplay( NULL, GX_FULLSCREEN );
					g_gxdp=GXGetDisplayProperties();
					gapixpitch=g_gxdp.cbxPitch;
					gapiypitch=g_gxdp.cbyPitch;
					GXOpenInput();
					main(5,g_hInst);
					GXCloseInput();
					GXCloseDisplay();
					GetWindowRect(hWnd, &rc);
					hWndTB=FindWindow(TEXT("HHTaskbar"),NULL);
					SHFullScreen( hWnd, SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON + SHFS_SHOWSTARTICON );
					CommandBar_Show(g_hwndCB,TRUE);
					ShowWindow( hWndTB, SW_SHOW );
					MoveWindow( hWnd, rc.left, 	rc.top + 26,rc.right, rc.bottom - 2 * 26,TRUE );
					CommandBar_Show(g_hwndCB,TRUE);
					ReadTosVer();
					reseted=1;
					SaveOptions();
					LoadOptions();
				}
			}
			break;
		case ID_KEY_CONFIG:
			GXOpenInput();
			DialogBox(g_hInst, (LPCTSTR)IDD_KEYCFG, hWnd, (DLGPROC)Keycfg);
			GXCloseInput();
			SaveOptions();
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_CREATE:
			g_hwndCB = CreateRpCommandBar(hWnd);
            // Initialize the shell activate info structure
            memset (&s_sai, 0, sizeof (s_sai));
            s_sai.cbSize = sizeof (s_sai);
			lastic=GetTickCount();
			break;
		case WM_PAINT:
			RECT		rtTop, rtBottom;
			hdc = BeginPaint(hWnd, &ps);
			GetClientRect(hWnd, &rtTop);
			if (whichpng==0) BitBlt(hdc, 0, 0, 240, 268, g_hLogoDC, 0, 0, SRCCOPY);
			else BitBlt(hdc, 0, 0, 240, 268, g_hGreatDC, 0, 0, SRCCOPY);
			EndPaint(hWnd, &ps);
			break; 
		case WM_DESTROY:
			CommandBar_Destroy(g_hwndCB);
			PostQuitMessage(0);
			break;
		case WM_ACTIVATE:
            // Notify shell of our activate message
			SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
			break;
		case WM_SETTINGCHANGE:
			SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
			break;
        case WM_KILLFOCUS:
			GXSuspend();
			break;
			
		case WM_SETFOCUS:
			GXResume();
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

HWND CreateRpCommandBar(HWND hwnd)
{
	SHMENUBARINFO mbi;
	
	memset(&mbi, 0, sizeof(SHMENUBARINFO));
	mbi.cbSize     = sizeof(SHMENUBARINFO);
	mbi.hwndParent = hwnd;
	mbi.nToolBarId = IDM_MENU;
	mbi.hInstRes   = g_hInst;
	mbi.nBmpId     = 0;
	mbi.cBmpImages = 0;
	
	if (!SHCreateMenuBar(&mbi)) 
		return NULL;
	
	return mbi.hwndMB;
}

// Mesage handler for the Fdc box.
LRESULT CALLBACK Fdc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	SHINITDLGINFO shidi;
	BOOL   bTrans;
	
	HDC hdc;
	
	
	
	
	switch (message)
	{
	case WM_INITDIALOG:
		// Create a Done button and size it.  
		shidi.dwMask = SHIDIM_FLAGS;
		shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
		shidi.hDlg = hDlg;
		SHInitDialog(&shidi);
		hdc = GetDC( hDlg );
		sides=(ret>>16)&0xff;
		tracks=(ret>>8)&0xff;	
		sectors=(ret)&0xff;
		choice=0;
		SetDlgItemInt( hDlg, IDC_EDIT_SIDE, sides, TRUE );
		SetDlgItemInt( hDlg, IDC_EDIT_TRACKS, tracks, TRUE );
		SetDlgItemInt( hDlg, IDC_EDIT_SECTORS, sectors, TRUE );
		ReleaseDC( hDlg, hdc );
		return TRUE; 
		
	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK)
		{
			FDCchange(((ret>>24)<<24)|(sides<<16)|(tracks<<8)|(sectors));
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		break;
	case WM_VSCROLL:
		if (LOWORD(wParam) == 8)
		{
			if (choice == IDC_SPIN_SIDE)
			{
				if (HIWORD(wParam)>ptsides) sides=1;
				if (HIWORD(wParam)<ptsides) sides=2;
				if (HIWORD(wParam)==ptsides&&ptsides) sides=1;
				if (HIWORD(wParam)==ptsides&&!ptsides) sides=2;
				ptsides=HIWORD(wParam);
				choice=0;
			}
			if (choice == IDC_SPIN_TRACKS)
			{
				if (HIWORD(wParam)>pttracks) tracks--;
				if (HIWORD(wParam)<pttracks) tracks++;
				if (HIWORD(wParam)==pttracks&&pttracks) tracks--;
				if (HIWORD(wParam)==pttracks&&!pttracks) tracks++;
				pttracks=HIWORD(wParam);
				if (tracks>85) tracks=85;
				if (tracks<79) tracks=79;
				choice=0;
			}
			if (choice == IDC_SPIN_SECTORS)
			{
				if (HIWORD(wParam)>ptsectors) sectors--;
				if (HIWORD(wParam)<ptsectors) sectors++;
				if (HIWORD(wParam)==ptsectors&&ptsectors) sectors--;
				if (HIWORD(wParam)==ptsectors&&!ptsectors) sectors++;
				ptsectors=HIWORD(wParam);
				if (sectors>12) sectors=12;
				if (sectors<9) sectors=9;
				choice=0;
			}
			hdc = GetDC( hDlg );
			SetDlgItemInt( hDlg, IDC_EDIT_SIDE, sides, TRUE );
			SetDlgItemInt( hDlg, IDC_EDIT_TRACKS, tracks, TRUE );
			SetDlgItemInt( hDlg, IDC_EDIT_SECTORS, sectors, TRUE );
			ReleaseDC( hDlg, hdc );
			
		}
		break;
	default:
		if (LOWORD(wParam) == IDC_SPIN_SIDE)
		{
			choice=IDC_SPIN_SIDE;
		}
		if (LOWORD(wParam) == IDC_SPIN_TRACKS)
		{
			choice=IDC_SPIN_TRACKS;
		}
		if (LOWORD(wParam) == IDC_SPIN_SECTORS)
		{
			choice=IDC_SPIN_SECTORS;
		}
		break;
		
	}
    return FALSE;
}

// Mesage handler for the Fdc box.
LRESULT CALLBACK JoySensibility(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	SHINITDLGINFO shidi;
	BOOL   bTrans;
	
	HDC hdc;
	
	
	
	
	switch (message)
	{
	case WM_INITDIALOG:
		// Create a Done button and size it.  
		shidi.dwMask = SHIDIM_FLAGS;
		shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
		shidi.hDlg = hDlg;
		SHInitDialog(&shidi);
		hdc = GetDC( hDlg );
		SetDlgItemInt( hDlg, IDC_EDIT_SENSIBILITY, joy_sensibility, TRUE );
		ReleaseDC( hDlg, hdc );
		return TRUE; 
		
	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK)
		{
			SaveOptions();
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		break;
	case WM_VSCROLL:
		if (LOWORD(wParam) == 8)
		{
			if (HIWORD(wParam)>ptsensibility) joy_sensibility--;
			if (HIWORD(wParam)<ptsensibility) joy_sensibility++;
			if (HIWORD(wParam)==ptsensibility&&ptsensibility) joy_sensibility--;
			if (HIWORD(wParam)==ptsensibility&&!ptsensibility) joy_sensibility++;
			ptsensibility=HIWORD(wParam);
			if (joy_sensibility>50) joy_sensibility=50;
			if (joy_sensibility<5) joy_sensibility=5;
			hdc = GetDC( hDlg );
			SetDlgItemInt( hDlg, IDC_EDIT_SENSIBILITY, joy_sensibility, TRUE );
			ReleaseDC( hDlg, hdc );
			
		}
		break;
	default:
		break;
		
	}
    return FALSE;
}
// Mesage handler for the Fdc box.
LRESULT CALLBACK Autofirespeed(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	SHINITDLGINFO shidi;
	BOOL   bTrans;
	
	HDC hdc;
	
	
	
	
	switch (message)
	{
	case WM_INITDIALOG:
		// Create a Done button and size it.  
		shidi.dwMask = SHIDIM_FLAGS;
		shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
		shidi.hDlg = hDlg;
		SHInitDialog(&shidi);
		hdc = GetDC( hDlg );
		SetDlgItemInt( hDlg, IDC_EDIT_AUTOFIRESPEED, autofirespeed, TRUE );
		ReleaseDC( hDlg, hdc );
		return TRUE; 
		
	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK)
		{
			SaveOptions();
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		break;
	case WM_VSCROLL:
		if (LOWORD(wParam) == 8)
		{
			if (HIWORD(wParam)>ptautofire) autofirespeed--;
			if (HIWORD(wParam)<ptautofire) autofirespeed++;
			if (HIWORD(wParam)==ptautofire&&ptautofire) autofirespeed--;
			if (HIWORD(wParam)==ptautofire&&!ptautofire) autofirespeed++;
			ptautofire=HIWORD(wParam);
			if (autofirespeed>50) autofirespeed=50;
			if (autofirespeed<0) autofirespeed=0;
			hdc = GetDC( hDlg );
			SetDlgItemInt( hDlg, IDC_EDIT_AUTOFIRESPEED, autofirespeed, TRUE );
			ReleaseDC( hDlg, hdc );
			
		}
		break;
	default:
		break;
		
	}
    return FALSE;
}

unsigned int hexstring2num(char *s) {
	unsigned int n;
	int r;
	r = sscanf(s, "%x", &n); 
	return n;
}

void ReadTosVer(){
	FILE *roms;
	uint8 tosnum1,tosnum2;
	uint16 tosnum;
	char strbuf[10];
	TCHAR sbW[256];
	
	HMENU hMenu;
	
	memset (&strbuf, 0, sizeof (strbuf));
	memset (&sbW, 0, sizeof (sbW));
	if (NULL == (roms = fopen ((const char *)PathName, "rb"))) {
		sprintf((char*)PathName,"");
	}else{	
		fread (&tosnum, 1, 2, roms);
		fread (&tosnum1, 1, 1, roms);
		fread (&tosnum2, 1, 1, roms);
		(void) fclose (roms);
		if (tosnum1!=1){
			memset (&PathName, 0, sizeof (PathName));
			MessageBox(g_hMainDlg,L"Warning!!! Tos Image Not Compatible or Not Valid",L"Disclaimer:",MB_OK);
		}else{
			if (tosnum2!=0&&tosnum2!=2&&tosnum2!=4){
				memset (&PathName, 0, sizeof (PathName));
				MessageBox(g_hMainDlg,L"Warning!!! Tos Image Not Compatible or Not Valid",L"Disclaimer:",MB_OK);			
			}
		}
		
		
		sprintf(strbuf,"Tos. %01d.%02d",tosnum1,tosnum2);
		ConvertCToT(sbW,strbuf);
		hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
		DeleteMenu(hMenu,ID_TOS_VERSION,MF_BYCOMMAND);
		InsertMenu(hMenu,ID_TOS_ROM,MF_BYCOMMAND|MF_STRING|MF_GRAYED ,ID_TOS_VERSION, sbW);
		DrawMenuBar(g_hwndCB); 
	}
}


void storepatchs(char *file,Patchs *patches){
	int index,curpatch,i,curlineadr;
	char hexnum[8];
	index=0;
	curpatch=0;
	while (index<4096*2){
		if (file[index]=='['){
			index++;
			i=0;
			while (file[index]!=13){
				patches[curpatch].name[i++]=file[index++];
			}
			index++;
			patches[curpatch].name[i]=0;
			patches[curpatch].realtime=0;
			curlineadr=0;
		}
		if (file[index]==']'){
			index++;
			curpatch++;
		}
		if (file[index]=='-'){
			index++;
			i=0;
			memset (&hexnum, 0, sizeof (hexnum));
			while (file[index]!=','){
				hexnum[i++]=file[index++];
			}
			patches[curpatch].addr[curlineadr]=hexstring2num(hexnum);
			index++;
			i=0;
			memset (&hexnum, 0, sizeof (hexnum));
			while (file[index]!=13){
				hexnum[i++]=file[index++];
			}
			patches[curpatch].word[curlineadr]=hexstring2num(hexnum);
			index++;
			curlineadr++;
		}
		if (file[index]=='#'){
			index++;
			i=0;
			memset (&hexnum, 0, sizeof (hexnum));
			while (file[index]!=','){
				hexnum[i++]=file[index++];
			}
			patches[curpatch].addr[curlineadr]=hexstring2num(hexnum);
			index++;
			i=0;
			memset (&hexnum, 0, sizeof (hexnum));
			while (file[index]!=13){
				hexnum[i++]=file[index++];
			}
			patches[curpatch].word[curlineadr]=hexstring2num(hexnum);
			index++;
			curlineadr++;
			patches[curpatch].realtime=1;
		}
		if (file[index]=='*'){
			index++;
			i=0;
			while (file[index]!=13){
				patches[curpatch].comment[i++]=file[index++];
			}
			index++;
			patches[curpatch].comment[i]=0;	
		}
		index++;
		
		
	}
}
void InitProcessList(HWND hwListBox){
	TCHAR sbW[256],RootPath[1024];
	int i;
	FILE *patchfile;
	int fsize;
	char patchf[400];
	FILE *logfile;
	char	Msg[400];
	
	
	memset (&sbW, 0, sizeof (sbW));
	memset (&Patch, 0, sizeof (Patch));
	memset (&patchf, 0, sizeof (patchf));
	memset (&PatchFile, 0, sizeof (PatchFile));
	memset (&RootPath[0], 0, sizeof (RootPath));
	wcscpy(RootPath,ApplFilepath);
	i=wcslen(RootPath)-1;
	while ((char )(RootPath[i])!='\\') RootPath[i--]=0;
	wcscat(RootPath,L"patch.txt");
	ConvertTToC(patchf,RootPath);
	patchfile = fopen(patchf,"rb");
	/*
	logfile = fopen("\\trace.txt","at");
	sprintf(Msg,"%s\n",patchf);
	fwrite(Msg,strlen(Msg),1,logfile);
	fflush (logfile);
	fclose(logfile);
	*/
	if (patchfile!=NULL){
		fseek(patchfile,0,SEEK_END);
		fsize = ftell(patchfile);
		fseek(patchfile,0,SEEK_SET);
		fread(PatchFile,1,fsize,patchfile);
		fclose(patchfile);
		storepatchs(PatchFile,&Patch[0]);
		
	}
	
	// reset listbox
	ListBox_ResetContent(hwListBox);
	ListBox_SetCurSel(hwListBox, -1);
	nSelectedPatch = 0;
	// init task list
	//int nIdx = ListBox_AddString(hwListBox, L"Jumping Jackson");
	//Store tlist index as itemdata  
	//ListBox_SetItemData(hwListBox, nIdx, i);
	// add the task list 
	i=0;
	while (strlen(Patch[i].name)>0){
		memset (&sbW, 0, sizeof (sbW));
		ConvertCToT(sbW,Patch[i].name);
		// add to listbox
		int nIdx = ListBox_AddString(hwListBox, sbW);
		//Store tlist index as itemdata  
		ListBox_SetItemData(hwListBox, nIdx, i);
		i++;
	}
	
}

// Mesage handler for the Fdc box.
LRESULT CALLBACK ApplyingPatch(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	SHINITDLGINFO shidi;
	BOOL   bTrans;
	TCHAR sbW[256];
	HDC hdc;
	int i;
	
	
	
	switch (message)
	{
	case WM_INITDIALOG:
		g_hMainDlg = hDlg;
		InitProcessList(GetDlgItem(hDlg, IDC_LIST1));
		memset (&sbW, 0, sizeof (sbW));
		Edit_SetText(GetDlgItem(hDlg, IDC_EDIT2), sbW);
		//EnableWindow(GetDlgItem(g_hMainDlg, IDAPPLY), FALSE);
		// Create a Done button and size it.  
		shidi.dwMask = SHIDIM_FLAGS;
		shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLG;
		shidi.hDlg = hDlg;
		SHInitDialog(&shidi);
		hdc = GetDC( hDlg );
		ReleaseDC( hDlg, hdc );
		return TRUE; 
		
        break;
	case WM_COMMAND:
		if (LOWORD(wParam) == IDC_LIST1)
		{
			nSelectedPatch = -1;
			int nIdx = ListBox_GetCurSel(GetDlgItem(hDlg, IDC_LIST1));
			nSelectedPatch = ListBox_GetItemData(GetDlgItem(hDlg, IDC_LIST1), nIdx);
			memset (&sbW, 0, sizeof (sbW));
			ConvertCToT(sbW,Patch[nSelectedPatch].comment);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT2), sbW);
			//EnableWindow(GetDlgItem(g_hMainDlg, IDAPPLY), TRUE);
		}
		if (LOWORD(wParam) == IDAPPLY)
		{
			if (nSelectedPatch>=0){
				PatchSelected=nSelectedPatch;
				if (Patch[PatchSelected].realtime==1)	RealTimePatch=1;
				else 		RealTimePatch=0;
				if (!RealTimePatch){
					i=0;
					while (Patch[nSelectedPatch].addr[i]!=0){
						SetMemW(Patch[nSelectedPatch].addr[i],Patch[nSelectedPatch].word[i]);
						i++;
					}
				}
				
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
		}
		if (LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		
		break;
	default:
		break;
		
	}
    return FALSE;
}





LRESULT CALLBACK Keycfg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	SHINITDLGINFO shidi;
	BOOL   bTrans;
	TCHAR sbW[256];
	char strbuf[10];
	HDC hdc;
	int i,choosed;
	
	
	
	switch (message)
	{
	case WM_INITDIALOG:
		g_hMainDlg = hDlg;
		memset (&strbuf, 0, sizeof (strbuf));
		memset (&sbW, 0, sizeof (sbW));
		_itoa(key_joy_up,strbuf,10);
		ConvertCToT(sbW,strbuf);
		Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_JOYUP), sbW);
		memset (&strbuf, 0, sizeof (strbuf));
		memset (&sbW, 0, sizeof (sbW));
		_itoa(key_joy_down,strbuf,10);
		ConvertCToT(sbW,strbuf);
		Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_JOYDOWN), sbW);
		memset (&strbuf, 0, sizeof (strbuf));
		memset (&sbW, 0, sizeof (sbW));
		_itoa(key_joy_left,strbuf,10);
		ConvertCToT(sbW,strbuf);
		Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_JOYLEFT), sbW);
		memset (&strbuf, 0, sizeof (strbuf));
		memset (&sbW, 0, sizeof (sbW));
		_itoa(key_joy_right,strbuf,10);
		ConvertCToT(sbW,strbuf);
		Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_JOYRIGHT), sbW);
		memset (&strbuf, 0, sizeof (strbuf));
		memset (&sbW, 0, sizeof (sbW));
		_itoa(key_joy_fire,strbuf,10);
		ConvertCToT(sbW,strbuf);
		Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_JOYFIRE), sbW);
		memset (&strbuf, 0, sizeof (strbuf));
		memset (&sbW, 0, sizeof (sbW));
		_itoa(key_mouse_left,strbuf,10);
		ConvertCToT(sbW,strbuf);
		Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_MOUSELEFT), sbW);
		memset (&strbuf, 0, sizeof (strbuf));
		memset (&sbW, 0, sizeof (sbW));
		_itoa(key_mouse_right,strbuf,10);
		ConvertCToT(sbW,strbuf);
		Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_MOUSERIGHT), sbW);
		memset (&strbuf, 0, sizeof (strbuf));
		memset (&sbW, 0, sizeof (sbW));
		_itoa(key_mouse_calib,strbuf,10);
		ConvertCToT(sbW,strbuf);
		Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_MOUSECALIB), sbW);
		memset (&strbuf, 0, sizeof (strbuf));
		memset (&sbW, 0, sizeof (sbW));
		_itoa(key_disk_change,strbuf,10);
		ConvertCToT(sbW,strbuf);
		Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_DISKCHANGE), sbW);
		//EnableWindow(GetDlgItem(g_hMainDlg, IDAPPLY), FALSE);
		// Create a Done button and size it.  
		shidi.dwMask = SHIDIM_FLAGS;
		shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
		shidi.hDlg = hDlg;
		SHInitDialog(&shidi);
		hdc = GetDC( hDlg );
		ReleaseDC( hDlg, hdc );
		return TRUE; 
		
        break;
	case WM_COMMAND:
		if (LOWORD(wParam) == IDC_BUTTON_JOYUP){
			choosed=0;
			while(!choosed){
				for (i=0;i<256;i++){
					if (GetAsyncKeyState(i)<0){
						if (i!=1&&i!=VK_LWIN&&i!=VK_F21){
							choosed=1;
							break;
						}
					}
				}
			}
			while (GetAsyncKeyState(i)<0);
			key_joy_up=i;
			memset (&sbW, 0, sizeof (sbW));
			memset (&strbuf, 0, sizeof (strbuf));
			_itoa(key_joy_up,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_JOYUP), sbW);				
		}
		
		if (LOWORD(wParam) == IDC_BUTTON_JOYDOWN){
			choosed=0;
			while(!choosed){
				for (i=0;i<256;i++){
					if (GetAsyncKeyState(i)<0){
						if (i!=1&&i!=VK_LWIN&&i!=VK_F21){
							choosed=1;
							break;
						}
					}
				}
			}
			while (GetAsyncKeyState(i)<0);
			key_joy_down=i;
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_joy_down,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_JOYDOWN), sbW);				
		}
		if (LOWORD(wParam) == IDC_BUTTON_JOYLEFT){
			choosed=0;
			while(!choosed){
				for (i=0;i<256;i++){
					if (GetAsyncKeyState(i)<0){
						if (i!=1&&i!=VK_LWIN&&i!=VK_F21){
							choosed=1;
							break;
						}
					}
				}
			}
			while (GetAsyncKeyState(i)<0);
			key_joy_left=i;
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_joy_left,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_JOYLEFT), sbW);				
		}
		if (LOWORD(wParam) == IDC_BUTTON_JOYRIGHT){
			choosed=0;
			while(!choosed){
				for (i=0;i<256;i++){
					if (GetAsyncKeyState(i)<0){
						if (i!=1&&i!=VK_LWIN&&i!=VK_F21){
							choosed=1;
							break;
						}
					}
				}
			}
			while (GetAsyncKeyState(i)<0);
			key_joy_right=i;
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_joy_right,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_JOYRIGHT), sbW);				
		}
		if (LOWORD(wParam) == IDC_BUTTON_JOYFIRE){
			choosed=0;
			while(!choosed){
				for (i=0;i<256;i++){
					if (GetAsyncKeyState(i)<0){
						if (i!=1&&i!=VK_LWIN&&i!=VK_F21){
							choosed=1;
							break;
						}
					}
				}
			}
			while (GetAsyncKeyState(i)<0);
			key_joy_fire=i;
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_joy_fire,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_JOYFIRE), sbW);				
		}
		if (LOWORD(wParam) == IDC_BUTTON_MOUSELEFT){
			choosed=0;
			while(!choosed){
				for (i=0;i<256;i++){
					if (GetAsyncKeyState(i)<0){
						if (i!=1&&i!=VK_LWIN&&i!=VK_F21){
							choosed=1;
							break;
						}
					}
				}
			}
			while (GetAsyncKeyState(i)<0);
			key_mouse_left=i;
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_mouse_left,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_MOUSELEFT), sbW);				
		}
		if (LOWORD(wParam) == IDC_BUTTON_MOUSERIGHT){
			choosed=0;
			while(!choosed){
				for (i=0;i<256;i++){
					if (GetAsyncKeyState(i)<0){
						if (i!=1&&i!=VK_LWIN&&i!=VK_F21){
							choosed=1;
							break;
						}
					}
				}
			}
			while (GetAsyncKeyState(i)<0);
			key_mouse_right=i;
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_mouse_right,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_MOUSERIGHT), sbW);				
		}
		if (LOWORD(wParam) == IDC_BUTTON_MOUSECALIB){
			choosed=0;
			while(!choosed){
				for (i=0;i<256;i++){
					if (GetAsyncKeyState(i)<0){
						if (i!=1&&i!=VK_LWIN&&i!=VK_F21){
							choosed=1;
							break;
						}
					}
				}
			}
			while (GetAsyncKeyState(i)<0);
			key_mouse_calib=i;
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_mouse_calib,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_MOUSECALIB), sbW);				
		}
		if (LOWORD(wParam) == IDC_BUTTON_DISKCHANGE){
			choosed=0;
			while(!choosed){
				for (i=0;i<256;i++){
					if (GetAsyncKeyState(i)<0){
						if (i!=1&&i!=VK_LWIN&&i!=VK_F21){
							choosed=1;
							break;
						}
					}
				}
			}
			while (GetAsyncKeyState(i)<0);
			key_disk_change=i;
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_disk_change,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_DISKCHANGE), sbW);				
		}
		if (LOWORD(wParam) == IDC_RESET){
			key_joy_up=255;
			key_joy_down=255;
			key_joy_left=255;
			key_joy_right=255;
			key_joy_fire=255;
			key_mouse_left=255;
			key_mouse_right=255;
			key_mouse_calib=255;
			key_disk_change=255;
			g_hMainDlg = hDlg;
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_joy_up,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_JOYUP), sbW);
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_joy_down,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_JOYDOWN), sbW);
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_joy_left,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_JOYLEFT), sbW);
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_joy_right,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_JOYRIGHT), sbW);
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_joy_fire,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_JOYFIRE), sbW);
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_mouse_left,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_MOUSELEFT), sbW);
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_mouse_right,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_MOUSERIGHT), sbW);
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_mouse_calib,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_MOUSECALIB), sbW);
			memset (&strbuf, 0, sizeof (strbuf));
			memset (&sbW, 0, sizeof (sbW));
			_itoa(key_disk_change,strbuf,10);
			ConvertCToT(sbW,strbuf);
			Edit_SetText(GetDlgItem(hDlg, IDC_EDIT_DISKCHANGE), sbW);
		}
		if (LOWORD(wParam) == IDOK){
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		
		break;
		default:
			break;
			
	}
    return FALSE;
}


//------------------------------------------------------------------------------
// LoadOptions
//      Loads stored options from the registry.
//------------------------------------------------------------------------------
#define SETKEYMASK(a,b) \
g_kmCurrent.dwKeyMask[RegQueryDword(hRootKey, a, g_kmDefault.GetKeyFromMask(b))] = b

bool LoadOptions()
{	int i;


HMENU hMenu;

HKEY  hRootKey = OpenRegistry();
bool  bReturn  = false;

if (!RegQueryBool (hRootKey, L"Disclaimer",false)){
	if (MessageBox(g_hMainDlg,L"Authors will\ntake no responsabilities\nin any degradation\nor destruction\nof your PocketPC\nwhen you use CastCE.\n\nAre you sure to continue?",L"Disclaimer:",MB_OKCANCEL)!=1) exit(1);
}
if (RegQueryBool (hRootKey, L"Sound")){
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_SOUND_ON, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem (hMenu, ID_SOUND_OFF, MF_BYCOMMAND | MF_UNCHECKED);
	checkedsound=1;
}else{
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_SOUND_OFF, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem (hMenu, ID_SOUND_ON, MF_BYCOMMAND | MF_UNCHECKED);
	checkedsound=0;
}
if (RegQueryBool (hRootKey, L"Sample")){
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_SOUND_SAMPLE_ON, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem (hMenu, ID_SOUND_SAMPLE_OFF, MF_BYCOMMAND | MF_UNCHECKED);
	checkedsample=1;
}else{
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_SOUND_SAMPLE_OFF, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem (hMenu, ID_SOUND_SAMPLE_ON, MF_BYCOMMAND | MF_UNCHECKED);
	checkedsample=0;
}

if (RegQueryBool (hRootKey, L"Speedbar")){
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_SPEEDBAR_ON, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem (hMenu, ID_SPEEDBAR_OFF, MF_BYCOMMAND | MF_UNCHECKED);
	speedbar=1;
}else{
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_SPEEDBAR_OFF, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem (hMenu, ID_SPEEDBAR_ON, MF_BYCOMMAND | MF_UNCHECKED);
	speedbar=0;
}

joynum=RegQueryDword (hRootKey, L"Joynum",255);
switch (joynum){
case 0xff:
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_JOY1, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem (hMenu, ID_JOY0, MF_BYCOMMAND | MF_UNCHECKED);
	break;
case 0xfe:
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_JOY0, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem (hMenu, ID_JOY1, MF_BYCOMMAND | MF_UNCHECKED);
	break;
}
RegQueryString (hRootKey, L"Tosrom",TOSFileName,L"");
i=0;
while ((char )(TOSFileName[i])!=0){
	PathName[i]=(char )(TOSFileName[i]);
	i++;
}
PathName[i]='\0';
ReadTosVer();


key_joy_up=RegQueryDword (hRootKey, L"KeyJoyUp",255);
key_joy_down=RegQueryDword (hRootKey, L"KeyJoyDown",255);
key_joy_left=RegQueryDword (hRootKey, L"KeyJoyLeft",255);
key_joy_right=RegQueryDword (hRootKey, L"KeyJoyRight",255);
key_joy_fire=RegQueryDword (hRootKey, L"KeyJoyFire",255);
key_mouse_left=RegQueryDword (hRootKey, L"KeyMouseLeft",255);
key_mouse_right=RegQueryDword (hRootKey, L"KeyMouseRight",255);
key_mouse_calib=RegQueryDword (hRootKey, L"KeyMouseCalib",255);
key_disk_change=RegQueryDword (hRootKey, L"KeyDiskChange",255);
compat=RegQueryDword (hRootKey, L"Compatibility",0);
switch (compat){
case 0:
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_COMPAT0, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem (hMenu, ID_COMPAT1, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_COMPAT2, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_COMPAT3, MF_BYCOMMAND | MF_UNCHECKED);
	break;
case 1:
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_COMPAT0, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_COMPAT1, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem (hMenu, ID_COMPAT2, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_COMPAT3, MF_BYCOMMAND | MF_UNCHECKED);
	break;
case 2:
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_COMPAT0, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_COMPAT1, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_COMPAT2, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem (hMenu, ID_COMPAT3, MF_BYCOMMAND | MF_UNCHECKED);
	break;
case 3:
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_COMPAT0, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_COMPAT1, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_COMPAT2, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_COMPAT3, MF_BYCOMMAND | MF_CHECKED);
	break;
}


nbframeskip=RegQueryDword (hRootKey, L"Frameskip",0);
switch (nbframeskip){
case 0:
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_FRAMESKIP_NONE, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_1, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_2, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_3, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_4, MF_BYCOMMAND | MF_UNCHECKED);
	break;
case 1:
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_FRAMESKIP_NONE, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_1, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_2, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_3, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_4, MF_BYCOMMAND | MF_UNCHECKED);
	break;
case 2:
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_FRAMESKIP_NONE, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_1, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_2, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_3, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_4, MF_BYCOMMAND | MF_UNCHECKED);
	break;
case 3:
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_FRAMESKIP_NONE, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_1, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_2, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_3, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_4, MF_BYCOMMAND | MF_UNCHECKED);
	break;
case 4:
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_FRAMESKIP_NONE, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_1, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_2, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_3, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem (hMenu, ID_FRAMESKIP_4, MF_BYCOMMAND | MF_CHECKED);
	break;
}
autoframeskip=RegQueryDword (hRootKey, L"AutoFrameskip",0);
hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
if (autoframeskip==1){
	autoframeskip=1;
	CheckMenuItem (hMenu, ID_AUTO_FRAMESKIP, MF_BYCOMMAND | MF_CHECKED);
}else{
	autoframeskip=0;
	CheckMenuItem (hMenu, ID_AUTO_FRAMESKIP, MF_BYCOMMAND | MF_UNCHECKED);
}
autofirespeed=RegQueryDword (hRootKey, L"autofirespeed",25);

if (RegQueryBool (hRootKey, L"JoyStylus",false)){
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_JOYSTYLUS, MF_BYCOMMAND | MF_CHECKED);
	joy_on_stylus=1;
}else{
	hMenu = (HMENU)SendMessage((g_hwndCB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)IDM_MAIN_COMMAND3);
	CheckMenuItem (hMenu, ID_JOYSTYLUS, MF_BYCOMMAND | MF_UNCHECKED);
	joy_on_stylus=0;
}

joy_sensibility=RegQueryDword (hRootKey, L"JoySensibility",20);

CloseRegistry(hRootKey);

return bReturn;
}

//------------------------------------------------------------------------------
// SaveOptions
//      Stores options into the registry.
//------------------------------------------------------------------------------
void SaveOptions()
{
	
	HKEY  hRootKey = OpenRegistry();
    RegSetBool (hRootKey, L"Disclaimer", true);
    RegSetBool (hRootKey, L"Sound",  (checkedsound == 1)?true:false);
    RegSetBool (hRootKey, L"Sample", (checkedsample == 1)?true:false);
    RegSetBool (hRootKey, L"Speedbar", (speedbar == 1)?true:false);
    RegSetDword (hRootKey, L"Joynum",  (DWORD)joynum);
    RegSetString (hRootKey, L"Tosrom",  TOSFileName);
    RegSetDword (hRootKey, L"KeyJoyUp",  (DWORD)key_joy_up);
    RegSetDword (hRootKey, L"KeyJoyDown",  (DWORD)key_joy_down);
    RegSetDword (hRootKey, L"KeyJoyLeft",  (DWORD)key_joy_left);
    RegSetDword (hRootKey, L"KeyJoyRight",  (DWORD)key_joy_right);
    RegSetDword (hRootKey, L"KeyJoyFire",  (DWORD)key_joy_fire);
    RegSetDword (hRootKey, L"KeyMouseLeft",  (DWORD)key_mouse_left);
    RegSetDword (hRootKey, L"KeyMouseRight",  (DWORD)key_mouse_right);
    RegSetDword (hRootKey, L"KeyMouseCalib",  (DWORD)key_mouse_calib);
    RegSetDword (hRootKey, L"KeyDiskChange",  (DWORD)key_disk_change);
    RegSetDword (hRootKey, L"Frameskip",  (DWORD)nbframeskip);
    RegSetDword (hRootKey, L"AutoFrameskip",  (DWORD)autoframeskip);
    RegSetDword (hRootKey, L"AutoFireSpeed",  (DWORD)autofirespeed);
    RegSetDword (hRootKey, L"Compatibility",  (DWORD)compat);
	RegSetBool (hRootKey, L"JoyStylus",  (joy_on_stylus == 1)?true:false);
    RegSetDword (hRootKey, L"JoySensibility",  (DWORD)joy_sensibility);
	CloseRegistry(hRootKey);
}


extern "C" void * GXBeginDraw_(void)
{
    return GXBeginDraw();
	
} /* GXBEGINDRAW() */

extern "C" int GXEndDraw_(void)
{
    return GXEndDraw();
	
} /* GXENDDRAW() */
