Fixing broken SoundGraph API build.
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
19 const int BUFFER_SIZE = 256;
22 HINSTANCE hInst = 0; // current instance
23 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
24 TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
25 HANDLE gThreadReceiver = INVALID_HANDLE_VALUE;
26 //HANDLE gThreadSender = INVALID_HANDLE_VALUE;
29 LPTSTR gPipeNameInbound = TEXT("\\\\.\\pipe\\sga-inbound");
30 LPTSTR gPipeNameOutbound = TEXT("\\\\.\\pipe\\sga-outbound");
31 HANDLE gPipeInbound=INVALID_HANDLE_VALUE;
32 HANDLE gPipeOutbound=INVALID_HANDLE_VALUE;
34 char gBufferReceiver[256];
35 char gBufferSender[256];
37 char gTextFirstLine[256];
38 char gTextSecondLine[256];
39 wchar_t gTextFirstLine16[256];
40 wchar_t gTextSecondLine16[256];
50 // Forward declarations of functions included in this code module:
51 ATOM MyRegisterClass(HINSTANCE hInstance);
52 BOOL InitInstance(HINSTANCE, int);
53 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
54 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
57 const char KRspDone[]="done:";
58 const char KRspPending[]="pending:";
59 const char KRspError[]="error:";
60 const char KRspClose[]="close:";
61 const char KRspOpen[]="open:";
62 const char KRspUnknown[]="unknown:";
63 const char KRspTrue[]="true:";
64 const char KRspFalse[]="false:";
65 //Supported incoming messages
66 const char KMsgInit[]="init:";
67 const char KMsgUninit[]="uninit:";
68 const char KMsgIsInit[]="is-init:";
69 const char KMsgIsPluginModeEnabled[]="is-plugin-mode-enabled:";
70 const char KMsgSetVfdText[]="set-vfd-text:";
71 const char KMsgQuit[]="quit:";
74 Create inbound pipe to receive messages.
76 bool CreateInboundPipe()
78 gPipeInbound = CreateNamedPipe(gPipeNameInbound, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE, PIPE_UNLIMITED_INSTANCES, BUFFER_SIZE, BUFFER_SIZE, 0, NULL);
80 //could not create named pipe
81 if (gPipeInbound==INVALID_HANDLE_VALUE)
84 //Will complete once a client connects
85 int success = ConnectNamedPipe(gPipeInbound, NULL);
87 //could not connect client
96 Create outbound pipe to send message
98 bool CreateOutboundPipe()
100 gPipeOutbound = CreateNamedPipe(gPipeNameOutbound, PIPE_ACCESS_OUTBOUND /*| FILE_FLAG_OVERLAPPED*/, PIPE_TYPE_BYTE, PIPE_UNLIMITED_INSTANCES, BUFFER_SIZE, BUFFER_SIZE, 0, NULL);
102 //could not create named pipe
103 if (gPipeOutbound==INVALID_HANDLE_VALUE)
106 //Will complete once a client connects
107 int success = ConnectNamedPipe(gPipeOutbound, NULL);
109 //could not connect client
117 Send a message to our server.
119 void SendMessageToServer(const char* aResponse)
123 sprintf(msg,"%s\n",aResponse);
124 OutputDebugStringA(msg);
125 WriteFile(gPipeOutbound, aResponse, strlen(aResponse), &cbWritten, NULL);
126 if (strlen(aResponse)!=cbWritten)
128 int err = GetLastError();
129 OutputDebugStringA("ERROR: WriteFile failure!\n");
136 Handle messages from our server.
138 void MessageReceived(const char* aMsg)
141 if (strcmp(aMsg,KMsgInit)==0)
143 //IMON API call need to be done from window thread for some reason
144 SendMessageToServer(KRspPending);
145 PostMessage(gWnd,WM_IMON_INIT,0,0);
147 else if (strcmp(aMsg,KMsgUninit)==0)
149 //IMON API call need to be done from window thread for some reason
150 SendMessageToServer(KRspPending);
151 PostMessage(gWnd,WM_IMON_UNINIT,0,0);
153 else if (strcmp(aMsg,KMsgIsPluginModeEnabled)==0)
155 //IMON API call need to be done from window thread for some reason
156 SendMessageToServer(KRspPending);
157 PostMessage(gWnd,WM_IMON_IS_PLUGIN_MODE_ENABLED,0,0);
159 else if (strcmp(aMsg,KMsgIsInit)==0)
161 //IMON API call need to be done from window thread for some reason
162 SendMessageToServer(KRspPending);
163 PostMessage(gWnd,WM_IMON_IS_INIT,0,0);
165 else if (strcmp(aMsg,KMsgQuit)==0)
168 SendMessageToServer(KRspDone);
171 else if (strstr(aMsg,KMsgSetVfdText)==aMsg)
173 int textLen=strlen(aMsg)-strlen(KMsgSetVfdText);
174 strncpy(gTextFirstLine,aMsg+strlen(KMsgSetVfdText),textLen);
175 gTextFirstLine[textLen]='\0';
176 //Check if we have a second line
177 char* ptr=strchr(gTextFirstLine,'\n');
180 *ptr='\0'; //Terminate our first line here
182 //Get our second line
184 strncpy(gTextSecondLine,ptr,textLen);
185 gTextSecondLine[textLen]='\0';
189 //No second line specified
190 gTextSecondLine[0]='\0';
191 gTextSecondLine16[0]='\0';
195 OutputDebugStringA(gTextFirstLine);
196 //int convertedChars = MultiByteToWideChar(CP_UTF8, 0, gTextFirstLine, -1, gTextFirstLine16, sizeof(gTextFirstLine16));
197 for (int i=0;i<=strlen(gTextFirstLine);i++)
199 unsigned char myChar=(unsigned char)gTextFirstLine[i];
200 gTextFirstLine16[i]=myChar;
202 OutputDebugStringW(gTextFirstLine16);
203 //Convert second line
204 OutputDebugStringA(gTextSecondLine);
205 //convertedChars = MultiByteToWideChar(CP_UTF8, 0, gTextSecondLine, -1, gTextSecondLine16, sizeof(gTextSecondLine16));
206 for (int i=0;i<=strlen(gTextSecondLine);i++)
208 unsigned char myChar=(unsigned char)gTextSecondLine[i];
209 gTextSecondLine16[i]=myChar;
212 OutputDebugStringW(gTextSecondLine16);
214 //IMON API call need to be done from window thread for some reason
215 SendMessageToServer(KRspPending);
218 PostMessage(gWnd,WM_IMON_INIT,0,0);
220 PostMessage(gWnd,WM_IMON_DISPLAY_SET_VFD_TEXT,0,0);
224 SendMessageToServer(KRspUnknown);
231 Connect our named pipes from here.
232 First connect our read pipe then our write pipe.
233 Then notify our server we are connected.
234 Then keep on waiting for incoming messages.
236 DWORD WINAPI ThreadReceiver( LPVOID lpParam )
240 //Keep on trying to connect on our read pipe
241 while (gPipeInbound==INVALID_HANDLE_VALUE && !gQuit)
243 OutputDebugStringA("Trying to connect...\n");
244 gPipeInbound=CreateFile(gPipeNameInbound, GENERIC_READ ,0,NULL,OPEN_EXISTING,0,NULL);
248 OutputDebugStringA("Read pipe open!\n");
249 //Now try connecting on our write pipe
250 gPipeOutbound=CreateFile(gPipeNameOutbound, GENERIC_WRITE ,0,NULL,OPEN_EXISTING,0,NULL);
251 if (gPipeOutbound==INVALID_HANDLE_VALUE)
253 int err=GetLastError();
254 OutputDebugStringA("ERROR: Write pipe failure!\n");
259 OutputDebugStringA("Write pipe opened.\n");
260 OutputDebugStringA("Connected.\n");
261 SendMessageToServer(KRspOpen);
267 CreateOutboundPipe();
273 BOOL success=ReadFile(gPipeInbound,gBufferReceiver,sizeof(gBufferReceiver),&cbRead, NULL);
276 gBufferReceiver[cbRead]='\0';
277 OutputDebugStringA(gBufferReceiver);
278 MessageReceived(gBufferReceiver);
280 //if (!success && GetLastError() != ERROR_MORE_DATA)
282 // OutputDebugStringA("Can't Read\n");
293 //Try tell our server
294 SendMessageToServer(KRspClose);
296 DisconnectNamedPipe(gPipeInbound);
297 CloseHandle(gPipeInbound);
298 gPipeInbound=INVALID_HANDLE_VALUE;
299 DisconnectNamedPipe(gPipeOutbound);
300 CloseHandle(gPipeOutbound);
301 gPipeOutbound=INVALID_HANDLE_VALUE;
302 //Quit our application
303 PostMessage(gWnd,WM_CLOSE,0,0);
312 int APIENTRY _tWinMain(HINSTANCE hInstance,
313 HINSTANCE hPrevInstance,
317 UNREFERENCED_PARAMETER(hPrevInstance);
318 UNREFERENCED_PARAMETER(lpCmdLine);
320 gTextFirstLine[0]='\0';
321 gTextSecondLine[0]='\0';
322 gTextFirstLine16[0]='\0';
323 gTextSecondLine16[0]='\0';
326 // TODO: Place code here.
330 // Initialize global strings
331 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
332 LoadString(hInstance, IDC_SOUNDGRAPHACCESS, szWindowClass, MAX_LOADSTRING);
333 MyRegisterClass(hInstance);
335 // Perform application initialization:
336 if (!InitInstance (hInstance, /*SW_HIDE*/ nCmdShow))
343 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS));
345 // Main message loop:
346 while (GetMessage(&msg, NULL, 0, 0))
348 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
350 TranslateMessage(&msg);
351 DispatchMessage(&msg);
355 return (int) msg.wParam;
360 void DisplayPluginMessage(UINT uErrCode, BOOL bError)
362 char* strErrMsg = "";
368 case DSPN_ERR_IN_USED: strErrMsg = ("Display Plug-in is Already Used by Other Application."); break;
369 case DSPN_ERR_HW_DISCONNECTED: strErrMsg = ("iMON HW is Not Connected."); break;
370 case DSPN_ERR_NOT_SUPPORTED_HW: strErrMsg = ("The Connected iMON HW doesn't Support Display Plug-in."); break;
371 case DSPN_ERR_PLUGIN_DISABLED: strErrMsg = ("Display Plug-in Mode Option is Disabled."); break;
372 case DSPN_ERR_IMON_NO_REPLY: strErrMsg = ("The Latest iMON is Not Installed or iMON Not Running."); break;
373 case DSPN_ERR_UNKNOWN: strErrMsg = ("Unknown Failure."); break;
380 case DSPNM_PLUGIN_SUCCEED: strErrMsg = ("Plug-in Mode Inited Successfully."); break;
381 case DSPNM_IMON_RESTARTED: strErrMsg = ("iMON Started and Plug-in Mode Inited."); break;
382 case DSPNM_HW_CONNECTED: strErrMsg = ("iMON HW Connected and Plug-in Mode Inited."); break;
388 OutputDebugStringA(strErrMsg);
393 sprintf(msg,"error:%s",strErrMsg);
397 sprintf(msg,"done:%s",strErrMsg);
399 SendMessageToServer(msg);
405 // FUNCTION: MyRegisterClass()
407 // PURPOSE: Registers the window class.
411 // This function and its usage are only necessary if you want this code
412 // to be compatible with Win32 systems prior to the 'RegisterClassEx'
413 // function that was added to Windows 95. It is important to call this function
414 // so that the application will get 'well formed' small icons associated
417 ATOM MyRegisterClass(HINSTANCE hInstance)
421 wcex.cbSize = sizeof(WNDCLASSEX);
423 wcex.style = CS_HREDRAW | CS_VREDRAW;
424 wcex.lpfnWndProc = WndProc;
427 wcex.hInstance = hInstance;
428 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SOUNDGRAPHACCESS));
429 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
430 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
431 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS);
432 wcex.lpszClassName = szWindowClass;
433 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
435 return RegisterClassEx(&wcex);
439 // FUNCTION: InitInstance(HINSTANCE, int)
441 // PURPOSE: Saves instance handle and creates main window
445 // In this function, we save the instance handle in a global variable and
446 // create and display the main program window.
448 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
452 hInst = hInstance; // Store instance handle in our global variable
454 gWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
455 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
462 ShowWindow(gWnd, nCmdShow);
469 // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
471 // PURPOSE: Processes messages for the main window.
473 // WM_COMMAND - process the application menu
474 // WM_PAINT - Paint the main window
475 // WM_DESTROY - post a quit message and return
478 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
487 //IMON_Display_Uninit();
488 //IMON_Display_Init(hWnd, WM_DSP_PLUGIN_NOTIFY);
489 gThreadReceiver = CreateThread( NULL, 0, ThreadReceiver, NULL/*data pointer*/, 0, NULL);
492 wmId = LOWORD(wParam);
493 wmEvent = HIWORD(wParam);
494 // Parse the menu selections:
498 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
504 return DefWindowProc(hWnd, message, wParam, lParam);
508 hdc = BeginPaint(hWnd, &ps);
509 // TODO: Add any drawing code here...
514 //To complete write op
515 if (gPipeOutbound!=INVALID_HANDLE_VALUE)
517 DisconnectNamedPipe(gPipeOutbound);
518 CloseHandle(gPipeOutbound);
519 gPipeOutbound=INVALID_HANDLE_VALUE;
521 //To complete read op
522 if (gPipeInbound!=INVALID_HANDLE_VALUE)
524 DisconnectNamedPipe(gPipeInbound);
525 CloseHandle(gPipeInbound);
526 gPipeInbound=INVALID_HANDLE_VALUE;
529 WaitForSingleObject(gThreadReceiver,INFINITE);
530 CloseHandle(gThreadReceiver);
531 //IMON_Display_Uninit();
537 IMON_Display_Uninit();
538 SendMessageToServer(KRspDone);
542 IMON_Display_Uninit();
543 IMON_Display_Init(hWnd, WM_DSP_PLUGIN_NOTIFY);
546 case WM_IMON_IS_PLUGIN_MODE_ENABLED:
547 if (IMON_Display_IsPluginModeEnabled()==DSP_S_IN_PLUGIN_MODE)
549 SendMessageToServer(KRspTrue);
553 SendMessageToServer(KRspFalse);
557 case WM_IMON_IS_INIT:
558 if (IMON_Display_IsInited()==DSP_S_INITED)
560 SendMessageToServer(KRspTrue);
564 SendMessageToServer(KRspFalse);
568 case WM_IMON_DISPLAY_SET_VFD_TEXT:
570 if (DSP_SUCCEEDED==IMON_Display_SetVfdText(gTextFirstLine16,gTextSecondLine16))
572 if (DSP_SUCCEEDED==IMON_Display_SetVfdText(gTextFirstLine,gTextSecondLine))
575 SendMessageToServer(KRspDone);
579 SendMessageToServer(KRspError);
583 case WM_DSP_PLUGIN_NOTIFY:
586 case DSPNM_PLUGIN_SUCCEED:
587 case DSPNM_IMON_RESTARTED:
588 case DSPNM_HW_CONNECTED:
590 //GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
591 m_bVfdConnected = FALSE;
592 m_bLcdConnected = FALSE;
593 if((lParam & DSPN_DSP_VFD) == DSPN_DSP_VFD) m_bVfdConnected = TRUE;
594 if((lParam & DSPN_DSP_LCD) == DSPN_DSP_LCD) m_bLcdConnected = TRUE;
597 DisplayPluginMessage(wParam, FALSE);
601 case DSPNM_PLUGIN_FAILED:
602 case DSPNM_HW_DISCONNECTED:
603 case DSPNM_IMON_CLOSED:
605 //GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
606 m_bVfdConnected = FALSE;
607 m_bLcdConnected = FALSE;
610 DisplayPluginMessage(lParam, TRUE);
614 case DSPNM_LCD_TEXT_SCROLL_DONE:
616 //TRACE(_T("LCD Text Scroll Finished.\n"));
623 return DefWindowProc(hWnd, message, wParam, lParam);
628 // Message handler for about box.
629 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
631 UNREFERENCED_PARAMETER(lParam);
635 return (INT_PTR)TRUE;
638 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
640 EndDialog(hDlg, LOWORD(wParam));
641 return (INT_PTR)TRUE;
645 return (INT_PTR)FALSE;