Sorting out both server side and client side. Now supporting more display APIs.
1 // SoundGraphAccess.cpp : Defines the entry point for the application.
5 #include "SoundGraphAccess.h"
6 #include "iMONDisplayAPI.h"
9 #define WM_DSP_PLUGIN_NOTIFY WM_APP + 1100
10 #define WM_IMON_INIT WM_APP + 1101
11 #define WM_IMON_UNINIT WM_APP + 1102
12 #define WM_IMON_IS_INIT WM_APP + 1103
13 #define WM_IMON_IS_PLUGIN_MODE_ENABLED WM_APP + 1104
16 #define MAX_LOADSTRING 100
20 HINSTANCE hInst = 0; // current instance
21 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
22 TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
23 HANDLE gThreadReceiver = INVALID_HANDLE_VALUE;
24 //HANDLE gThreadSender = INVALID_HANDLE_VALUE;
27 LPTSTR gPipeNameSender = TEXT("\\\\.\\pipe\\sga-sender");
28 LPTSTR gPipeNameReceiver = TEXT("\\\\.\\pipe\\sga-receiver");
29 HANDLE gPipeSender=INVALID_HANDLE_VALUE;
30 HANDLE gPipeReceiver=INVALID_HANDLE_VALUE;
32 char gBufferReceiver[256];
33 char gBufferSender[256];
42 // Forward declarations of functions included in this code module:
43 ATOM MyRegisterClass(HINSTANCE hInstance);
44 BOOL InitInstance(HINSTANCE, int);
45 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
46 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
49 const char KRspDone[]="done:";
50 const char KRspPending[]="pending:";
51 const char KRspError[]="error:";
52 const char KRspClose[]="close:";
53 const char KRspOpen[]="open:";
54 const char KRspUnknown[]="unknown:";
55 const char KRspTrue[]="true:";
56 const char KRspFalse[]="false:";
57 //Supported incoming messages
58 const char KMsgInit[]="init:";
59 const char KMsgUninit[]="uninit:";
60 const char KMsgIsInit[]="is-init:";
61 const char KMsgIsPluginModeEnabled[]="is-plugin-mode-enabled:";
64 Send a message to our server.
66 void SendMessageToServer(const char* aResponse)
70 sprintf(msg,"%s\n",aResponse);
71 OutputDebugStringA(msg);
72 WriteFile(gPipeSender, aResponse, strlen(aResponse), &cbWritten, NULL);
77 Handle messages from our server.
79 void MessageReceived(const char* aMsg)
82 if (strcmp(aMsg,KMsgInit)==0)
84 //IMON API call need to be done from window thread for some reason
85 SendMessageToServer(KRspPending);
86 PostMessage(gWnd,WM_IMON_INIT,0,0);
88 else if (strcmp(aMsg,KMsgUninit)==0)
90 //IMON API call need to be done from window thread for some reason
91 SendMessageToServer(KRspPending);
92 PostMessage(gWnd,WM_IMON_UNINIT,0,0);
94 else if (strcmp(aMsg,KMsgIsPluginModeEnabled)==0)
96 //IMON API call need to be done from window thread for some reason
97 SendMessageToServer(KRspPending);
98 PostMessage(gWnd,WM_IMON_IS_PLUGIN_MODE_ENABLED,0,0);
100 else if (strcmp(aMsg,KMsgIsInit)==0)
102 //IMON API call need to be done from window thread for some reason
103 SendMessageToServer(KRspPending);
104 PostMessage(gWnd,WM_IMON_IS_INIT,0,0);
108 SendMessageToServer(KRspUnknown);
115 Connect our named pipes from here.
116 First connect our read pipe then our write pipe.
117 Then notify our server we are connected.
118 Then keep on waiting for incoming messages.
120 DWORD WINAPI ThreadReceiver( LPVOID lpParam )
122 //Keep on trying to connect on our read pipe
123 while (gPipeReceiver==INVALID_HANDLE_VALUE && !gQuit)
125 OutputDebugStringA("Trying to connect...\n");
126 gPipeReceiver=CreateFile(gPipeNameReceiver, GENERIC_READ ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
130 OutputDebugStringA("Read pipe open!\n");
131 //Now try connecting on our write pipe
132 gPipeSender=CreateFile(gPipeNameSender, GENERIC_WRITE ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
133 if (gPipeSender==INVALID_HANDLE_VALUE)
135 OutputDebugStringA("ERROR: Write pipe failure!\n");
140 OutputDebugStringA("Write pipe opened.\n");
141 OutputDebugStringA("Connected.\n");
142 SendMessageToServer(KRspOpen);
149 BOOL success=ReadFile(gPipeReceiver,gBufferReceiver,sizeof(gBufferReceiver),&cbRead, NULL);
152 gBufferReceiver[cbRead]='\0';
153 OutputDebugStringA(gBufferReceiver);
154 MessageReceived(gBufferReceiver);
156 //if (!success && GetLastError() != ERROR_MORE_DATA)
158 // OutputDebugStringA("Can't Read\n");
169 //Try tell our server
170 SendMessageToServer(KRspClose);
172 CloseHandle(gPipeReceiver);
173 gPipeReceiver=INVALID_HANDLE_VALUE;
174 CloseHandle(gPipeSender);
175 gPipeSender=INVALID_HANDLE_VALUE;
176 //Quit our application
177 PostMessage(gWnd,WM_CLOSE,0,0);
186 int APIENTRY _tWinMain(HINSTANCE hInstance,
187 HINSTANCE hPrevInstance,
191 UNREFERENCED_PARAMETER(hPrevInstance);
192 UNREFERENCED_PARAMETER(lpCmdLine);
194 // TODO: Place code here.
198 // Initialize global strings
199 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
200 LoadString(hInstance, IDC_SOUNDGRAPHACCESS, szWindowClass, MAX_LOADSTRING);
201 MyRegisterClass(hInstance);
203 // Perform application initialization:
204 if (!InitInstance (hInstance, /*SW_HIDE*/ nCmdShow))
211 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS));
213 // Main message loop:
214 while (GetMessage(&msg, NULL, 0, 0))
216 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
218 TranslateMessage(&msg);
219 DispatchMessage(&msg);
223 return (int) msg.wParam;
228 void DisplayPluginMessage(UINT uErrCode, BOOL bError)
230 char* strErrMsg = "";
236 case DSPN_ERR_IN_USED: strErrMsg = ("Display Plug-in is Already Used by Other Application."); break;
237 case DSPN_ERR_HW_DISCONNECTED: strErrMsg = ("iMON HW is Not Connected."); break;
238 case DSPN_ERR_NOT_SUPPORTED_HW: strErrMsg = ("The Connected iMON HW doesn't Support Display Plug-in."); break;
239 case DSPN_ERR_PLUGIN_DISABLED: strErrMsg = ("Display Plug-in Mode Option is Disabled."); break;
240 case DSPN_ERR_IMON_NO_REPLY: strErrMsg = ("The Latest iMON is Not Installed or iMON Not Running."); break;
241 case DSPN_ERR_UNKNOWN: strErrMsg = ("Unknown Failure."); break;
248 case DSPNM_PLUGIN_SUCCEED: strErrMsg = ("Plug-in Mode Inited Successfully."); break;
249 case DSPNM_IMON_RESTARTED: strErrMsg = ("iMON Started and Plug-in Mode Inited."); break;
250 case DSPNM_HW_CONNECTED: strErrMsg = ("iMON HW Connected and Plug-in Mode Inited."); break;
254 OutputDebugStringA(strErrMsg);
259 sprintf(msg,"error:%s",strErrMsg);
263 sprintf(msg,"done:%s",strErrMsg);
265 SendMessageToServer(msg);
271 // FUNCTION: MyRegisterClass()
273 // PURPOSE: Registers the window class.
277 // This function and its usage are only necessary if you want this code
278 // to be compatible with Win32 systems prior to the 'RegisterClassEx'
279 // function that was added to Windows 95. It is important to call this function
280 // so that the application will get 'well formed' small icons associated
283 ATOM MyRegisterClass(HINSTANCE hInstance)
287 wcex.cbSize = sizeof(WNDCLASSEX);
289 wcex.style = CS_HREDRAW | CS_VREDRAW;
290 wcex.lpfnWndProc = WndProc;
293 wcex.hInstance = hInstance;
294 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SOUNDGRAPHACCESS));
295 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
296 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
297 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS);
298 wcex.lpszClassName = szWindowClass;
299 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
301 return RegisterClassEx(&wcex);
305 // FUNCTION: InitInstance(HINSTANCE, int)
307 // PURPOSE: Saves instance handle and creates main window
311 // In this function, we save the instance handle in a global variable and
312 // create and display the main program window.
314 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
318 hInst = hInstance; // Store instance handle in our global variable
320 gWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
321 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
328 ShowWindow(gWnd, nCmdShow);
335 // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
337 // PURPOSE: Processes messages for the main window.
339 // WM_COMMAND - process the application menu
340 // WM_PAINT - Paint the main window
341 // WM_DESTROY - post a quit message and return
344 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
353 //IMON_Display_Uninit();
354 //IMON_Display_Init(hWnd, WM_DSP_PLUGIN_NOTIFY);
355 gThreadReceiver = CreateThread( NULL, 0, ThreadReceiver, NULL/*data pointer*/, 0, NULL);
358 wmId = LOWORD(wParam);
359 wmEvent = HIWORD(wParam);
360 // Parse the menu selections:
364 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
370 return DefWindowProc(hWnd, message, wParam, lParam);
374 hdc = BeginPaint(hWnd, &ps);
375 // TODO: Add any drawing code here...
380 //To complete write op
381 if (gPipeSender!=INVALID_HANDLE_VALUE)
383 CloseHandle(gPipeSender);
384 gPipeSender=INVALID_HANDLE_VALUE;
386 //To complete read op
387 if (gPipeReceiver!=INVALID_HANDLE_VALUE)
389 CloseHandle(gPipeReceiver);
390 gPipeReceiver=INVALID_HANDLE_VALUE;
393 WaitForSingleObject(gThreadReceiver,INFINITE);
394 CloseHandle(gThreadReceiver);
395 //IMON_Display_Uninit();
400 IMON_Display_Uninit();
401 SendMessageToServer(KRspDone);
405 IMON_Display_Uninit();
406 IMON_Display_Init(hWnd, WM_DSP_PLUGIN_NOTIFY);
409 case WM_IMON_IS_PLUGIN_MODE_ENABLED:
410 if (IMON_Display_IsPluginModeEnabled()==DSP_S_IN_PLUGIN_MODE)
412 SendMessageToServer(KRspTrue);
416 SendMessageToServer(KRspFalse);
420 case WM_IMON_IS_INIT:
421 if (IMON_Display_IsInited()==DSP_S_INITED)
423 SendMessageToServer(KRspTrue);
427 SendMessageToServer(KRspFalse);
432 case WM_DSP_PLUGIN_NOTIFY:
435 case DSPNM_PLUGIN_SUCCEED:
436 case DSPNM_IMON_RESTARTED:
437 case DSPNM_HW_CONNECTED:
439 //GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
440 m_bVfdConnected = FALSE;
441 m_bLcdConnected = FALSE;
442 if((lParam & DSPN_DSP_VFD) == DSPN_DSP_VFD) m_bVfdConnected = TRUE;
443 if((lParam & DSPN_DSP_LCD) == DSPN_DSP_LCD) m_bLcdConnected = TRUE;
446 DisplayPluginMessage(wParam, FALSE);
450 case DSPNM_PLUGIN_FAILED:
451 case DSPNM_HW_DISCONNECTED:
452 case DSPNM_IMON_CLOSED:
454 //GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
455 m_bVfdConnected = FALSE;
456 m_bLcdConnected = FALSE;
459 DisplayPluginMessage(lParam, TRUE);
463 case DSPNM_LCD_TEXT_SCROLL_DONE:
465 //TRACE(_T("LCD Text Scroll Finished.\n"));
472 return DefWindowProc(hWnd, message, wParam, lParam);
477 // Message handler for about box.
478 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
480 UNREFERENCED_PARAMETER(lParam);
484 return (INT_PTR)TRUE;
487 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
489 EndDialog(hDlg, LOWORD(wParam));
490 return (INT_PTR)TRUE;
494 return (INT_PTR)FALSE;