SoundGraphAccess.cpp
changeset 0 a77691c40066
child 2 ebd2fb40b033
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/SoundGraphAccess.cpp	Thu Feb 07 01:10:03 2013 +0100
     1.3 @@ -0,0 +1,364 @@
     1.4 +// SoundGraphAccess.cpp : Defines the entry point for the application.
     1.5 +//
     1.6 +
     1.7 +#include "stdafx.h"
     1.8 +#include "SoundGraphAccess.h"
     1.9 +#include "iMONDisplayAPI.h"
    1.10 +
    1.11 +#define WM_DSP_PLUGIN_NOTIFY	WM_APP + 1121
    1.12 +#define MAX_LOADSTRING 100
    1.13 +
    1.14 +
    1.15 +// Global Variables:
    1.16 +HINSTANCE hInst = 0;								// current instance
    1.17 +TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
    1.18 +TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name
    1.19 +HANDLE gThreadReceiver = 0;
    1.20 +HANDLE gThreadSender = 0;
    1.21 +BOOL gQuit=FALSE;
    1.22 +
    1.23 +LPTSTR gPipeNameSender = TEXT("\\\\.\\pipe\\sga-sender"); 
    1.24 +LPTSTR gPipeNameReceiver = TEXT("\\\\.\\pipe\\sga-receiver"); 
    1.25 +HANDLE gPipeSender=0;
    1.26 +HANDLE gPipeReceiver=0;
    1.27 +
    1.28 +char gBufferReceiver[256];
    1.29 +char gBufferSender[256];
    1.30 +
    1.31 +
    1.32 +//
    1.33 +BOOL m_bVfdConnected;
    1.34 +BOOL m_bLcdConnected;
    1.35 +
    1.36 +// Forward declarations of functions included in this code module:
    1.37 +ATOM				MyRegisterClass(HINSTANCE hInstance);
    1.38 +BOOL				InitInstance(HINSTANCE, int);
    1.39 +LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
    1.40 +INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
    1.41 +
    1.42 +
    1.43 +
    1.44 +
    1.45 +/**
    1.46 +Read our pipe from this thread
    1.47 +*/
    1.48 +DWORD WINAPI ThreadReceiver( LPVOID lpParam )
    1.49 +	{
    1.50 +	//Create our pipe and listen
    1.51 +	gPipeReceiver=CreateFile(gPipeNameReceiver,	GENERIC_READ ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
    1.52 +
    1.53 +	while(!gQuit)
    1.54 +		{
    1.55 +		DWORD cbRead;
    1.56 +		BOOL success=ReadFile(gPipeReceiver,gBufferReceiver,sizeof(gBufferReceiver),&cbRead, NULL); 
    1.57 +		if(success)
    1.58 +			{			
    1.59 +			gBufferReceiver[cbRead]='\0';
    1.60 +			OutputDebugStringA(gBufferReceiver);
    1.61 +			}
    1.62 +		if (!success && GetLastError() != ERROR_MORE_DATA) 
    1.63 +			{
    1.64 +			OutputDebugStringA("Can't Read\n");
    1.65 +			}
    1.66 +		//
    1.67 +		Sleep(500);
    1.68 +		}
    1.69 +
    1.70 +	if (gPipeReceiver!=0)
    1.71 +		{
    1.72 +		CloseHandle(gPipeReceiver);	
    1.73 +		gPipeReceiver=0;
    1.74 +		}
    1.75 +	
    1.76 +	return 0;
    1.77 +	}
    1.78 +
    1.79 +
    1.80 +/**
    1.81 +Write to our pipe from this thread
    1.82 +*/
    1.83 +DWORD WINAPI ThreadSender( LPVOID lpParam )
    1.84 +	{
    1.85 +	gPipeSender=CreateFile(gPipeNameSender,	GENERIC_WRITE ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
    1.86 +	
    1.87 +
    1.88 +	if (gPipeSender!=0)
    1.89 +		{
    1.90 +		CloseHandle(gPipeSender);
    1.91 +		gPipeSender=0;
    1.92 +		}
    1.93 +
    1.94 +	return 0;
    1.95 +	}
    1.96 +
    1.97 +
    1.98 +/**
    1.99 +*/
   1.100 +int APIENTRY _tWinMain(HINSTANCE hInstance,
   1.101 +                     HINSTANCE hPrevInstance,
   1.102 +                     LPTSTR    lpCmdLine,
   1.103 +                     int       nCmdShow)
   1.104 +{
   1.105 +	UNREFERENCED_PARAMETER(hPrevInstance);
   1.106 +	UNREFERENCED_PARAMETER(lpCmdLine);
   1.107 +
   1.108 +	// TODO: Place code here.
   1.109 +	MSG msg;
   1.110 +	HACCEL hAccelTable;
   1.111 +
   1.112 +	// Initialize global strings
   1.113 +	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
   1.114 +	LoadString(hInstance, IDC_SOUNDGRAPHACCESS, szWindowClass, MAX_LOADSTRING);
   1.115 +	MyRegisterClass(hInstance);
   1.116 +
   1.117 +	// Perform application initialization:
   1.118 +	if (!InitInstance (hInstance, /*SW_HIDE*/ nCmdShow))
   1.119 +		{
   1.120 +		return FALSE;
   1.121 +		}
   1.122 +
   1.123 +
   1.124 +
   1.125 +	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS));
   1.126 +
   1.127 +	// Main message loop:
   1.128 +	while (GetMessage(&msg, NULL, 0, 0))
   1.129 +		{
   1.130 +		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
   1.131 +			{
   1.132 +			TranslateMessage(&msg);
   1.133 +			DispatchMessage(&msg);
   1.134 +			}
   1.135 +		}
   1.136 +
   1.137 +	return (int) msg.wParam;
   1.138 +
   1.139 +	}
   1.140 +
   1.141 +
   1.142 +void DisplayPluginMessage(UINT uErrCode, BOOL bError)
   1.143 +	{
   1.144 +	LPCTSTR strErrMsg = _T("");
   1.145 +
   1.146 +	if(bError)
   1.147 +		{
   1.148 +		switch(uErrCode)
   1.149 +			{
   1.150 +			case DSPN_ERR_IN_USED:			strErrMsg = _T("Display Plug-in is Already Used by Other Application.");		break;
   1.151 +			case DSPN_ERR_HW_DISCONNECTED:	strErrMsg = _T("iMON HW is Not Connected.");									break;
   1.152 +			case DSPN_ERR_NOT_SUPPORTED_HW:	strErrMsg = _T("The Connected iMON HW doesn't Support Display Plug-in.");		break;
   1.153 +			case DSPN_ERR_PLUGIN_DISABLED:	strErrMsg = _T("Display Plug-in Mode Option is Disabled.");						break;
   1.154 +			case DSPN_ERR_IMON_NO_REPLY:	strErrMsg = _T("The Latest iMON is Not Installed or iMON Not Running.");		break;
   1.155 +			case DSPN_ERR_UNKNOWN:			strErrMsg = _T("Unknown Failure.");												break;
   1.156 +			}
   1.157 +		}
   1.158 +	else
   1.159 +		{
   1.160 +		switch(uErrCode)
   1.161 +			{
   1.162 +			case DSPNM_PLUGIN_SUCCEED:		strErrMsg = _T("Plug-in Mode Inited Successfully.");		break;
   1.163 +			case DSPNM_IMON_RESTARTED:		strErrMsg = _T("iMON Started and Plug-in Mode Inited.");		break;
   1.164 +			case DSPNM_HW_CONNECTED:		strErrMsg = _T("iMON HW Connected and Plug-in Mode Inited.");	break;
   1.165 +			}
   1.166 +		}
   1.167 +	//
   1.168 +	OutputDebugString((LPCTSTR)strErrMsg);
   1.169 +	}
   1.170 +
   1.171 +
   1.172 +
   1.173 +//
   1.174 +//  FUNCTION: MyRegisterClass()
   1.175 +//
   1.176 +//  PURPOSE: Registers the window class.
   1.177 +//
   1.178 +//  COMMENTS:
   1.179 +//
   1.180 +//    This function and its usage are only necessary if you want this code
   1.181 +//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
   1.182 +//    function that was added to Windows 95. It is important to call this function
   1.183 +//    so that the application will get 'well formed' small icons associated
   1.184 +//    with it.
   1.185 +//
   1.186 +ATOM MyRegisterClass(HINSTANCE hInstance)
   1.187 +{
   1.188 +	WNDCLASSEX wcex;
   1.189 +
   1.190 +	wcex.cbSize = sizeof(WNDCLASSEX);
   1.191 +
   1.192 +	wcex.style			= CS_HREDRAW | CS_VREDRAW;
   1.193 +	wcex.lpfnWndProc	= WndProc;
   1.194 +	wcex.cbClsExtra		= 0;
   1.195 +	wcex.cbWndExtra		= 0;
   1.196 +	wcex.hInstance		= hInstance;
   1.197 +	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SOUNDGRAPHACCESS));
   1.198 +	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
   1.199 +	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
   1.200 +	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS);
   1.201 +	wcex.lpszClassName	= szWindowClass;
   1.202 +	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
   1.203 +
   1.204 +	return RegisterClassEx(&wcex);
   1.205 +}
   1.206 +
   1.207 +//
   1.208 +//   FUNCTION: InitInstance(HINSTANCE, int)
   1.209 +//
   1.210 +//   PURPOSE: Saves instance handle and creates main window
   1.211 +//
   1.212 +//   COMMENTS:
   1.213 +//
   1.214 +//        In this function, we save the instance handle in a global variable and
   1.215 +//        create and display the main program window.
   1.216 +//
   1.217 +BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
   1.218 +{
   1.219 +   HWND hWnd;
   1.220 +
   1.221 +   hInst = hInstance; // Store instance handle in our global variable
   1.222 +
   1.223 +   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
   1.224 +      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
   1.225 +
   1.226 +   if (!hWnd)
   1.227 +   {
   1.228 +      return FALSE;
   1.229 +   }
   1.230 +
   1.231 +   ShowWindow(hWnd, nCmdShow);
   1.232 +   UpdateWindow(hWnd);
   1.233 +
   1.234 +   return TRUE;
   1.235 +}
   1.236 +
   1.237 +//
   1.238 +//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
   1.239 +//
   1.240 +//  PURPOSE:  Processes messages for the main window.
   1.241 +//
   1.242 +//  WM_COMMAND	- process the application menu
   1.243 +//  WM_PAINT	- Paint the main window
   1.244 +//  WM_DESTROY	- post a quit message and return
   1.245 +//
   1.246 +//
   1.247 +LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
   1.248 +{
   1.249 +	int wmId, wmEvent;
   1.250 +	PAINTSTRUCT ps;
   1.251 +	HDC hdc;
   1.252 +
   1.253 +	switch (message)
   1.254 +	{
   1.255 +	case WM_CREATE:
   1.256 +		//IMON_Display_Uninit();
   1.257 +		//IMON_Display_Init(hWnd, WM_DSP_PLUGIN_NOTIFY);
   1.258 +		gThreadReceiver = CreateThread( NULL, 0, ThreadReceiver, NULL/*data pointer*/, 0, NULL);
   1.259 +		gThreadSender = CreateThread( NULL, 0, ThreadSender, NULL/*data pointer*/, 0, NULL);
   1.260 +		break;
   1.261 +	case WM_COMMAND:
   1.262 +		wmId    = LOWORD(wParam);
   1.263 +		wmEvent = HIWORD(wParam);
   1.264 +		// Parse the menu selections:
   1.265 +		switch (wmId)
   1.266 +		{
   1.267 +		case IDM_ABOUT:
   1.268 +			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
   1.269 +			break;
   1.270 +		case IDM_EXIT:
   1.271 +			DestroyWindow(hWnd);
   1.272 +			break;
   1.273 +		default:
   1.274 +			return DefWindowProc(hWnd, message, wParam, lParam);
   1.275 +		}
   1.276 +		break;
   1.277 +	case WM_PAINT:
   1.278 +		hdc = BeginPaint(hWnd, &ps);
   1.279 +		// TODO: Add any drawing code here...
   1.280 +		EndPaint(hWnd, &ps);
   1.281 +		break;
   1.282 +	case WM_DESTROY:
   1.283 +		gQuit=TRUE;
   1.284 +		//To complete write op
   1.285 +		if (gPipeSender!=0)
   1.286 +			{
   1.287 +			CloseHandle(gPipeSender);
   1.288 +			gPipeSender=0;
   1.289 +			}
   1.290 +		//To complete read op
   1.291 +		if (gPipeReceiver!=0)
   1.292 +			{
   1.293 +			CloseHandle(gPipeReceiver);
   1.294 +			gPipeReceiver=0;
   1.295 +			}
   1.296 +		WaitForSingleObject(gThreadSender,INFINITE);
   1.297 +		WaitForSingleObject(gThreadReceiver,INFINITE);
   1.298 +		CloseHandle(gThreadSender);
   1.299 +		CloseHandle(gThreadReceiver);
   1.300 +		//IMON_Display_Uninit();
   1.301 +		PostQuitMessage(0);
   1.302 +		break;
   1.303 +	case WM_DSP_PLUGIN_NOTIFY:
   1.304 +		switch(wParam)
   1.305 +			{
   1.306 +			case DSPNM_PLUGIN_SUCCEED:
   1.307 +			case DSPNM_IMON_RESTARTED:
   1.308 +			case DSPNM_HW_CONNECTED:
   1.309 +				{
   1.310 +				//GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
   1.311 +				m_bVfdConnected = FALSE;
   1.312 +				m_bLcdConnected = FALSE;
   1.313 +				if((lParam & DSPN_DSP_VFD) == DSPN_DSP_VFD)		m_bVfdConnected = TRUE;
   1.314 +				if((lParam & DSPN_DSP_LCD) == DSPN_DSP_LCD)		m_bLcdConnected = TRUE;
   1.315 +				//UpdateControlUI();
   1.316 +
   1.317 +				DisplayPluginMessage(wParam, FALSE);
   1.318 +				}
   1.319 +				break;
   1.320 +
   1.321 +			case DSPNM_PLUGIN_FAILED:
   1.322 +			case DSPNM_HW_DISCONNECTED:
   1.323 +			case DSPNM_IMON_CLOSED:
   1.324 +				{
   1.325 +				//GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
   1.326 +				m_bVfdConnected = FALSE;
   1.327 +				m_bLcdConnected = FALSE;
   1.328 +				//UpdateControlUI();
   1.329 +
   1.330 +				DisplayPluginMessage(lParam, TRUE);
   1.331 +				}
   1.332 +				break;
   1.333 +
   1.334 +			case DSPNM_LCD_TEXT_SCROLL_DONE:
   1.335 +				{
   1.336 +				//TRACE(_T("LCD Text Scroll Finished.\n"));
   1.337 +				}
   1.338 +				break;
   1.339 +			}
   1.340 +		return 0;
   1.341 +		break;
   1.342 +	default:
   1.343 +		return DefWindowProc(hWnd, message, wParam, lParam);
   1.344 +	}
   1.345 +	return 0;
   1.346 +}
   1.347 +
   1.348 +// Message handler for about box.
   1.349 +INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
   1.350 +{
   1.351 +	UNREFERENCED_PARAMETER(lParam);
   1.352 +	switch (message)
   1.353 +	{
   1.354 +	case WM_INITDIALOG:
   1.355 +		return (INT_PTR)TRUE;
   1.356 +
   1.357 +	case WM_COMMAND:
   1.358 +		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
   1.359 +		{
   1.360 +			EndDialog(hDlg, LOWORD(wParam));
   1.361 +			return (INT_PTR)TRUE;
   1.362 +		}
   1.363 +		break;
   1.364 +	}
   1.365 +	return (INT_PTR)FALSE;
   1.366 +}
   1.367 +