diff -r 000000000000 -r a77691c40066 SoundGraphAccess.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SoundGraphAccess.cpp Thu Feb 07 01:10:03 2013 +0100 @@ -0,0 +1,364 @@ +// SoundGraphAccess.cpp : Defines the entry point for the application. +// + +#include "stdafx.h" +#include "SoundGraphAccess.h" +#include "iMONDisplayAPI.h" + +#define WM_DSP_PLUGIN_NOTIFY WM_APP + 1121 +#define MAX_LOADSTRING 100 + + +// Global Variables: +HINSTANCE hInst = 0; // current instance +TCHAR szTitle[MAX_LOADSTRING]; // The title bar text +TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name +HANDLE gThreadReceiver = 0; +HANDLE gThreadSender = 0; +BOOL gQuit=FALSE; + +LPTSTR gPipeNameSender = TEXT("\\\\.\\pipe\\sga-sender"); +LPTSTR gPipeNameReceiver = TEXT("\\\\.\\pipe\\sga-receiver"); +HANDLE gPipeSender=0; +HANDLE gPipeReceiver=0; + +char gBufferReceiver[256]; +char gBufferSender[256]; + + +// +BOOL m_bVfdConnected; +BOOL m_bLcdConnected; + +// Forward declarations of functions included in this code module: +ATOM MyRegisterClass(HINSTANCE hInstance); +BOOL InitInstance(HINSTANCE, int); +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); +INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); + + + + +/** +Read our pipe from this thread +*/ +DWORD WINAPI ThreadReceiver( LPVOID lpParam ) + { + //Create our pipe and listen + gPipeReceiver=CreateFile(gPipeNameReceiver, GENERIC_READ ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL); + + while(!gQuit) + { + DWORD cbRead; + BOOL success=ReadFile(gPipeReceiver,gBufferReceiver,sizeof(gBufferReceiver),&cbRead, NULL); + if(success) + { + gBufferReceiver[cbRead]='\0'; + OutputDebugStringA(gBufferReceiver); + } + if (!success && GetLastError() != ERROR_MORE_DATA) + { + OutputDebugStringA("Can't Read\n"); + } + // + Sleep(500); + } + + if (gPipeReceiver!=0) + { + CloseHandle(gPipeReceiver); + gPipeReceiver=0; + } + + return 0; + } + + +/** +Write to our pipe from this thread +*/ +DWORD WINAPI ThreadSender( LPVOID lpParam ) + { + gPipeSender=CreateFile(gPipeNameSender, GENERIC_WRITE ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL); + + + if (gPipeSender!=0) + { + CloseHandle(gPipeSender); + gPipeSender=0; + } + + return 0; + } + + +/** +*/ +int APIENTRY _tWinMain(HINSTANCE hInstance, + HINSTANCE hPrevInstance, + LPTSTR lpCmdLine, + int nCmdShow) +{ + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(lpCmdLine); + + // TODO: Place code here. + MSG msg; + HACCEL hAccelTable; + + // Initialize global strings + LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); + LoadString(hInstance, IDC_SOUNDGRAPHACCESS, szWindowClass, MAX_LOADSTRING); + MyRegisterClass(hInstance); + + // Perform application initialization: + if (!InitInstance (hInstance, /*SW_HIDE*/ nCmdShow)) + { + return FALSE; + } + + + + hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS)); + + // Main message loop: + while (GetMessage(&msg, NULL, 0, 0)) + { + if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } + + return (int) msg.wParam; + + } + + +void DisplayPluginMessage(UINT uErrCode, BOOL bError) + { + LPCTSTR strErrMsg = _T(""); + + if(bError) + { + switch(uErrCode) + { + case DSPN_ERR_IN_USED: strErrMsg = _T("Display Plug-in is Already Used by Other Application."); break; + case DSPN_ERR_HW_DISCONNECTED: strErrMsg = _T("iMON HW is Not Connected."); break; + case DSPN_ERR_NOT_SUPPORTED_HW: strErrMsg = _T("The Connected iMON HW doesn't Support Display Plug-in."); break; + case DSPN_ERR_PLUGIN_DISABLED: strErrMsg = _T("Display Plug-in Mode Option is Disabled."); break; + case DSPN_ERR_IMON_NO_REPLY: strErrMsg = _T("The Latest iMON is Not Installed or iMON Not Running."); break; + case DSPN_ERR_UNKNOWN: strErrMsg = _T("Unknown Failure."); break; + } + } + else + { + switch(uErrCode) + { + case DSPNM_PLUGIN_SUCCEED: strErrMsg = _T("Plug-in Mode Inited Successfully."); break; + case DSPNM_IMON_RESTARTED: strErrMsg = _T("iMON Started and Plug-in Mode Inited."); break; + case DSPNM_HW_CONNECTED: strErrMsg = _T("iMON HW Connected and Plug-in Mode Inited."); break; + } + } + // + OutputDebugString((LPCTSTR)strErrMsg); + } + + + +// +// FUNCTION: MyRegisterClass() +// +// PURPOSE: Registers the window class. +// +// COMMENTS: +// +// This function and its usage are only necessary if you want this code +// to be compatible with Win32 systems prior to the 'RegisterClassEx' +// function that was added to Windows 95. It is important to call this function +// so that the application will get 'well formed' small icons associated +// with it. +// +ATOM MyRegisterClass(HINSTANCE hInstance) +{ + WNDCLASSEX wcex; + + wcex.cbSize = sizeof(WNDCLASSEX); + + wcex.style = CS_HREDRAW | CS_VREDRAW; + wcex.lpfnWndProc = WndProc; + wcex.cbClsExtra = 0; + wcex.cbWndExtra = 0; + wcex.hInstance = hInstance; + wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SOUNDGRAPHACCESS)); + wcex.hCursor = LoadCursor(NULL, IDC_ARROW); + wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); + wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS); + wcex.lpszClassName = szWindowClass; + wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); + + return RegisterClassEx(&wcex); +} + +// +// FUNCTION: InitInstance(HINSTANCE, 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; + + hInst = hInstance; // Store instance handle in our global variable + + hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); + + if (!hWnd) + { + return FALSE; + } + + ShowWindow(hWnd, nCmdShow); + UpdateWindow(hWnd); + + return TRUE; +} + +// +// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) +// +// 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) +{ + int wmId, wmEvent; + PAINTSTRUCT ps; + HDC hdc; + + switch (message) + { + case WM_CREATE: + //IMON_Display_Uninit(); + //IMON_Display_Init(hWnd, WM_DSP_PLUGIN_NOTIFY); + gThreadReceiver = CreateThread( NULL, 0, ThreadReceiver, NULL/*data pointer*/, 0, NULL); + gThreadSender = CreateThread( NULL, 0, ThreadSender, NULL/*data pointer*/, 0, NULL); + break; + case WM_COMMAND: + wmId = LOWORD(wParam); + wmEvent = HIWORD(wParam); + // Parse the menu selections: + switch (wmId) + { + case IDM_ABOUT: + DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); + break; + case IDM_EXIT: + DestroyWindow(hWnd); + break; + default: + return DefWindowProc(hWnd, message, wParam, lParam); + } + break; + case WM_PAINT: + hdc = BeginPaint(hWnd, &ps); + // TODO: Add any drawing code here... + EndPaint(hWnd, &ps); + break; + case WM_DESTROY: + gQuit=TRUE; + //To complete write op + if (gPipeSender!=0) + { + CloseHandle(gPipeSender); + gPipeSender=0; + } + //To complete read op + if (gPipeReceiver!=0) + { + CloseHandle(gPipeReceiver); + gPipeReceiver=0; + } + WaitForSingleObject(gThreadSender,INFINITE); + WaitForSingleObject(gThreadReceiver,INFINITE); + CloseHandle(gThreadSender); + CloseHandle(gThreadReceiver); + //IMON_Display_Uninit(); + PostQuitMessage(0); + break; + case WM_DSP_PLUGIN_NOTIFY: + switch(wParam) + { + case DSPNM_PLUGIN_SUCCEED: + case DSPNM_IMON_RESTARTED: + case DSPNM_HW_CONNECTED: + { + //GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE); + m_bVfdConnected = FALSE; + m_bLcdConnected = FALSE; + if((lParam & DSPN_DSP_VFD) == DSPN_DSP_VFD) m_bVfdConnected = TRUE; + if((lParam & DSPN_DSP_LCD) == DSPN_DSP_LCD) m_bLcdConnected = TRUE; + //UpdateControlUI(); + + DisplayPluginMessage(wParam, FALSE); + } + break; + + case DSPNM_PLUGIN_FAILED: + case DSPNM_HW_DISCONNECTED: + case DSPNM_IMON_CLOSED: + { + //GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE); + m_bVfdConnected = FALSE; + m_bLcdConnected = FALSE; + //UpdateControlUI(); + + DisplayPluginMessage(lParam, TRUE); + } + break; + + case DSPNM_LCD_TEXT_SCROLL_DONE: + { + //TRACE(_T("LCD Text Scroll Finished.\n")); + } + break; + } + return 0; + break; + default: + return DefWindowProc(hWnd, message, wParam, lParam); + } + return 0; +} + +// Message handler for about box. +INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) +{ + UNREFERENCED_PARAMETER(lParam); + switch (message) + { + case WM_INITDIALOG: + return (INT_PTR)TRUE; + + case WM_COMMAND: + if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) + { + EndDialog(hDlg, LOWORD(wParam)); + return (INT_PTR)TRUE; + } + break; + } + return (INT_PTR)FALSE; +} +