Basic support for VFD text API.
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
14 #define WM_IMON_DISPLAY_SET_VFD_TEXT WM_APP + 1105
17 #define MAX_LOADSTRING 100
21 HINSTANCE hInst = 0; // current instance
22 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
23 TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
24 HANDLE gThreadReceiver = INVALID_HANDLE_VALUE;
25 //HANDLE gThreadSender = INVALID_HANDLE_VALUE;
28 LPTSTR gPipeNameSender = TEXT("\\\\.\\pipe\\sga-sender");
29 LPTSTR gPipeNameReceiver = TEXT("\\\\.\\pipe\\sga-receiver");
30 HANDLE gPipeSender=INVALID_HANDLE_VALUE;
31 HANDLE gPipeReceiver=INVALID_HANDLE_VALUE;
33 char gBufferReceiver[256];
34 char gBufferSender[256];
36 char gTextFirstLine[256];
37 char gTextSecondLine[256];
38 wchar_t gTextFirstLine16[256];
39 wchar_t gTextSecondLine16[256];
48 // Forward declarations of functions included in this code module:
49 ATOM MyRegisterClass(HINSTANCE hInstance);
50 BOOL InitInstance(HINSTANCE, int);
51 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
52 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
55 const char KRspDone[]="done:";
56 const char KRspPending[]="pending:";
57 const char KRspError[]="error:";
58 const char KRspClose[]="close:";
59 const char KRspOpen[]="open:";
60 const char KRspUnknown[]="unknown:";
61 const char KRspTrue[]="true:";
62 const char KRspFalse[]="false:";
63 //Supported incoming messages
64 const char KMsgInit[]="init:";
65 const char KMsgUninit[]="uninit:";
66 const char KMsgIsInit[]="is-init:";
67 const char KMsgIsPluginModeEnabled[]="is-plugin-mode-enabled:";
68 const char KMsgSetVfdText[]="set-vfd-text:";
72 Send a message to our server.
74 void SendMessageToServer(const char* aResponse)
78 sprintf(msg,"%s\n",aResponse);
79 OutputDebugStringA(msg);
80 WriteFile(gPipeSender, aResponse, strlen(aResponse), &cbWritten, NULL);
85 Handle messages from our server.
87 void MessageReceived(const char* aMsg)
90 if (strcmp(aMsg,KMsgInit)==0)
92 //IMON API call need to be done from window thread for some reason
93 SendMessageToServer(KRspPending);
94 PostMessage(gWnd,WM_IMON_INIT,0,0);
96 else if (strcmp(aMsg,KMsgUninit)==0)
98 //IMON API call need to be done from window thread for some reason
99 SendMessageToServer(KRspPending);
100 PostMessage(gWnd,WM_IMON_UNINIT,0,0);
102 else if (strcmp(aMsg,KMsgIsPluginModeEnabled)==0)
104 //IMON API call need to be done from window thread for some reason
105 SendMessageToServer(KRspPending);
106 PostMessage(gWnd,WM_IMON_IS_PLUGIN_MODE_ENABLED,0,0);
108 else if (strcmp(aMsg,KMsgIsInit)==0)
110 //IMON API call need to be done from window thread for some reason
111 SendMessageToServer(KRspPending);
112 PostMessage(gWnd,WM_IMON_IS_INIT,0,0);
114 else if (strstr(aMsg,KMsgSetVfdText)==aMsg)
116 int textLen=strlen(aMsg)-strlen(KMsgSetVfdText);
117 strncpy(gTextFirstLine,aMsg+strlen(KMsgSetVfdText),textLen);
118 gTextFirstLine[textLen]='\0';
119 OutputDebugStringA(gTextFirstLine);
120 int convertedChars = MultiByteToWideChar(CP_UTF8, 0, gTextFirstLine, -1, gTextFirstLine16, sizeof(gTextFirstLine16));
121 OutputDebugString(gTextFirstLine16);
122 //IMON API call need to be done from window thread for some reason
123 SendMessageToServer(KRspPending);
124 PostMessage(gWnd,WM_IMON_DISPLAY_SET_VFD_TEXT,0,0);
128 SendMessageToServer(KRspUnknown);
135 Connect our named pipes from here.
136 First connect our read pipe then our write pipe.
137 Then notify our server we are connected.
138 Then keep on waiting for incoming messages.
140 DWORD WINAPI ThreadReceiver( LPVOID lpParam )
142 //Keep on trying to connect on our read pipe
143 while (gPipeReceiver==INVALID_HANDLE_VALUE && !gQuit)
145 OutputDebugStringA("Trying to connect...\n");
146 gPipeReceiver=CreateFile(gPipeNameReceiver, GENERIC_READ ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
150 OutputDebugStringA("Read pipe open!\n");
151 //Now try connecting on our write pipe
152 gPipeSender=CreateFile(gPipeNameSender, GENERIC_WRITE ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
153 if (gPipeSender==INVALID_HANDLE_VALUE)
155 OutputDebugStringA("ERROR: Write pipe failure!\n");
160 OutputDebugStringA("Write pipe opened.\n");
161 OutputDebugStringA("Connected.\n");
162 SendMessageToServer(KRspOpen);
169 BOOL success=ReadFile(gPipeReceiver,gBufferReceiver,sizeof(gBufferReceiver),&cbRead, NULL);
172 gBufferReceiver[cbRead]='\0';
173 OutputDebugStringA(gBufferReceiver);
174 MessageReceived(gBufferReceiver);
176 //if (!success && GetLastError() != ERROR_MORE_DATA)
178 // OutputDebugStringA("Can't Read\n");
189 //Try tell our server
190 SendMessageToServer(KRspClose);
192 CloseHandle(gPipeReceiver);
193 gPipeReceiver=INVALID_HANDLE_VALUE;
194 CloseHandle(gPipeSender);
195 gPipeSender=INVALID_HANDLE_VALUE;
196 //Quit our application
197 PostMessage(gWnd,WM_CLOSE,0,0);
206 int APIENTRY _tWinMain(HINSTANCE hInstance,
207 HINSTANCE hPrevInstance,
211 UNREFERENCED_PARAMETER(hPrevInstance);
212 UNREFERENCED_PARAMETER(lpCmdLine);
214 gTextFirstLine[0]='\0';
215 gTextSecondLine[0]='\0';
216 gTextFirstLine16[0]='\0';
217 gTextSecondLine16[0]='\0';
220 // TODO: Place code here.
224 // Initialize global strings
225 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
226 LoadString(hInstance, IDC_SOUNDGRAPHACCESS, szWindowClass, MAX_LOADSTRING);
227 MyRegisterClass(hInstance);
229 // Perform application initialization:
230 if (!InitInstance (hInstance, /*SW_HIDE*/ nCmdShow))
237 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS));
239 // Main message loop:
240 while (GetMessage(&msg, NULL, 0, 0))
242 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
244 TranslateMessage(&msg);
245 DispatchMessage(&msg);
249 return (int) msg.wParam;
254 void DisplayPluginMessage(UINT uErrCode, BOOL bError)
256 char* strErrMsg = "";
262 case DSPN_ERR_IN_USED: strErrMsg = ("Display Plug-in is Already Used by Other Application."); break;
263 case DSPN_ERR_HW_DISCONNECTED: strErrMsg = ("iMON HW is Not Connected."); break;
264 case DSPN_ERR_NOT_SUPPORTED_HW: strErrMsg = ("The Connected iMON HW doesn't Support Display Plug-in."); break;
265 case DSPN_ERR_PLUGIN_DISABLED: strErrMsg = ("Display Plug-in Mode Option is Disabled."); break;
266 case DSPN_ERR_IMON_NO_REPLY: strErrMsg = ("The Latest iMON is Not Installed or iMON Not Running."); break;
267 case DSPN_ERR_UNKNOWN: strErrMsg = ("Unknown Failure."); break;
274 case DSPNM_PLUGIN_SUCCEED: strErrMsg = ("Plug-in Mode Inited Successfully."); break;
275 case DSPNM_IMON_RESTARTED: strErrMsg = ("iMON Started and Plug-in Mode Inited."); break;
276 case DSPNM_HW_CONNECTED: strErrMsg = ("iMON HW Connected and Plug-in Mode Inited."); break;
280 OutputDebugStringA(strErrMsg);
285 sprintf(msg,"error:%s",strErrMsg);
289 sprintf(msg,"done:%s",strErrMsg);
291 SendMessageToServer(msg);
297 // FUNCTION: MyRegisterClass()
299 // PURPOSE: Registers the window class.
303 // This function and its usage are only necessary if you want this code
304 // to be compatible with Win32 systems prior to the 'RegisterClassEx'
305 // function that was added to Windows 95. It is important to call this function
306 // so that the application will get 'well formed' small icons associated
309 ATOM MyRegisterClass(HINSTANCE hInstance)
313 wcex.cbSize = sizeof(WNDCLASSEX);
315 wcex.style = CS_HREDRAW | CS_VREDRAW;
316 wcex.lpfnWndProc = WndProc;
319 wcex.hInstance = hInstance;
320 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SOUNDGRAPHACCESS));
321 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
322 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
323 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS);
324 wcex.lpszClassName = szWindowClass;
325 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
327 return RegisterClassEx(&wcex);
331 // FUNCTION: InitInstance(HINSTANCE, int)
333 // PURPOSE: Saves instance handle and creates main window
337 // In this function, we save the instance handle in a global variable and
338 // create and display the main program window.
340 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
344 hInst = hInstance; // Store instance handle in our global variable
346 gWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
347 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
354 ShowWindow(gWnd, nCmdShow);
361 // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
363 // PURPOSE: Processes messages for the main window.
365 // WM_COMMAND - process the application menu
366 // WM_PAINT - Paint the main window
367 // WM_DESTROY - post a quit message and return
370 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
379 //IMON_Display_Uninit();
380 //IMON_Display_Init(hWnd, WM_DSP_PLUGIN_NOTIFY);
381 gThreadReceiver = CreateThread( NULL, 0, ThreadReceiver, NULL/*data pointer*/, 0, NULL);
384 wmId = LOWORD(wParam);
385 wmEvent = HIWORD(wParam);
386 // Parse the menu selections:
390 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
396 return DefWindowProc(hWnd, message, wParam, lParam);
400 hdc = BeginPaint(hWnd, &ps);
401 // TODO: Add any drawing code here...
406 //To complete write op
407 if (gPipeSender!=INVALID_HANDLE_VALUE)
409 CloseHandle(gPipeSender);
410 gPipeSender=INVALID_HANDLE_VALUE;
412 //To complete read op
413 if (gPipeReceiver!=INVALID_HANDLE_VALUE)
415 CloseHandle(gPipeReceiver);
416 gPipeReceiver=INVALID_HANDLE_VALUE;
419 WaitForSingleObject(gThreadReceiver,INFINITE);
420 CloseHandle(gThreadReceiver);
421 //IMON_Display_Uninit();
426 IMON_Display_Uninit();
427 SendMessageToServer(KRspDone);
431 IMON_Display_Uninit();
432 IMON_Display_Init(hWnd, WM_DSP_PLUGIN_NOTIFY);
435 case WM_IMON_IS_PLUGIN_MODE_ENABLED:
436 if (IMON_Display_IsPluginModeEnabled()==DSP_S_IN_PLUGIN_MODE)
438 SendMessageToServer(KRspTrue);
442 SendMessageToServer(KRspFalse);
446 case WM_IMON_IS_INIT:
447 if (IMON_Display_IsInited()==DSP_S_INITED)
449 SendMessageToServer(KRspTrue);
453 SendMessageToServer(KRspFalse);
457 case WM_IMON_DISPLAY_SET_VFD_TEXT:
458 if (DSP_SUCCEEDED==IMON_Display_SetVfdText(gTextFirstLine16,gTextSecondLine16))
460 SendMessageToServer(KRspDone);
464 SendMessageToServer(KRspError);
468 case WM_DSP_PLUGIN_NOTIFY:
471 case DSPNM_PLUGIN_SUCCEED:
472 case DSPNM_IMON_RESTARTED:
473 case DSPNM_HW_CONNECTED:
475 //GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
476 m_bVfdConnected = FALSE;
477 m_bLcdConnected = FALSE;
478 if((lParam & DSPN_DSP_VFD) == DSPN_DSP_VFD) m_bVfdConnected = TRUE;
479 if((lParam & DSPN_DSP_LCD) == DSPN_DSP_LCD) m_bLcdConnected = TRUE;
482 DisplayPluginMessage(wParam, FALSE);
486 case DSPNM_PLUGIN_FAILED:
487 case DSPNM_HW_DISCONNECTED:
488 case DSPNM_IMON_CLOSED:
490 //GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
491 m_bVfdConnected = FALSE;
492 m_bLcdConnected = FALSE;
495 DisplayPluginMessage(lParam, TRUE);
499 case DSPNM_LCD_TEXT_SCROLL_DONE:
501 //TRACE(_T("LCD Text Scroll Finished.\n"));
508 return DefWindowProc(hWnd, message, wParam, lParam);
513 // Message handler for about box.
514 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
516 UNREFERENCED_PARAMETER(lParam);
520 return (INT_PTR)TRUE;
523 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
525 EndDialog(hDlg, LOWORD(wParam));
526 return (INT_PTR)TRUE;
530 return (INT_PTR)FALSE;