SoundGraphAccess.cpp
author StephaneLenclud
Sat, 13 Apr 2013 00:45:13 +0200
changeset 7 ad71cf47d037
parent 6 f1413622948f
child 8 3031cd3ebd1e
permissions -rw-r--r--
Now parses second line of text from set VFD text messages.
sl@0
     1
// SoundGraphAccess.cpp : Defines the entry point for the application.
sl@0
     2
//
sl@0
     3
sl@0
     4
#include "stdafx.h"
sl@0
     5
#include "SoundGraphAccess.h"
sl@0
     6
#include "iMONDisplayAPI.h"
lenclud@3
     7
#include <stdio.h>
sl@0
     8
lenclud@3
     9
#define WM_DSP_PLUGIN_NOTIFY			WM_APP + 1100
lenclud@3
    10
#define WM_IMON_INIT					WM_APP + 1101
lenclud@3
    11
#define WM_IMON_UNINIT					WM_APP + 1102
lenclud@3
    12
#define WM_IMON_IS_INIT					WM_APP + 1103
lenclud@3
    13
#define WM_IMON_IS_PLUGIN_MODE_ENABLED	WM_APP + 1104
lenclud@4
    14
#define WM_IMON_DISPLAY_SET_VFD_TEXT	WM_APP + 1105
lenclud@3
    15
lenclud@3
    16
//
sl@0
    17
#define MAX_LOADSTRING 100
sl@0
    18
StephaneLenclud@6
    19
const int BUFFER_SIZE = 256;
sl@0
    20
sl@0
    21
// Global Variables:
sl@0
    22
HINSTANCE hInst = 0;								// current instance
sl@0
    23
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
sl@0
    24
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name
lenclud@3
    25
HANDLE gThreadReceiver = INVALID_HANDLE_VALUE;
lenclud@3
    26
//HANDLE gThreadSender = INVALID_HANDLE_VALUE;
sl@0
    27
BOOL gQuit=FALSE;
sl@0
    28
StephaneLenclud@6
    29
LPTSTR gPipeNameInbound = TEXT("\\\\.\\pipe\\sga-inbound"); 
StephaneLenclud@6
    30
LPTSTR gPipeNameOutbound = TEXT("\\\\.\\pipe\\sga-outbound"); 
StephaneLenclud@6
    31
HANDLE gPipeInbound=INVALID_HANDLE_VALUE;
StephaneLenclud@6
    32
HANDLE gPipeOutbound=INVALID_HANDLE_VALUE;
sl@0
    33
sl@0
    34
char gBufferReceiver[256];
sl@0
    35
char gBufferSender[256];
lenclud@4
    36
//
lenclud@4
    37
char gTextFirstLine[256];
lenclud@4
    38
char gTextSecondLine[256];
lenclud@4
    39
wchar_t gTextFirstLine16[256];
lenclud@4
    40
wchar_t gTextSecondLine16[256];
sl@0
    41
lenclud@2
    42
//
lenclud@2
    43
HWND gWnd;
sl@0
    44
sl@0
    45
//
sl@0
    46
BOOL m_bVfdConnected;
sl@0
    47
BOOL m_bLcdConnected;
StephaneLenclud@5
    48
BOOL m_IsInit=FALSE;
sl@0
    49
sl@0
    50
// Forward declarations of functions included in this code module:
sl@0
    51
ATOM				MyRegisterClass(HINSTANCE hInstance);
sl@0
    52
BOOL				InitInstance(HINSTANCE, int);
sl@0
    53
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
sl@0
    54
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
sl@0
    55
lenclud@3
    56
//Supported responses
lenclud@3
    57
const char KRspDone[]="done:";
lenclud@3
    58
const char KRspPending[]="pending:";
lenclud@3
    59
const char KRspError[]="error:";
lenclud@3
    60
const char KRspClose[]="close:";
lenclud@3
    61
const char KRspOpen[]="open:";
lenclud@3
    62
const char KRspUnknown[]="unknown:";
lenclud@3
    63
const char KRspTrue[]="true:";
lenclud@3
    64
const char KRspFalse[]="false:";
lenclud@3
    65
//Supported incoming messages
lenclud@3
    66
const char KMsgInit[]="init:";
lenclud@3
    67
const char KMsgUninit[]="uninit:";
lenclud@3
    68
const char KMsgIsInit[]="is-init:";
lenclud@3
    69
const char KMsgIsPluginModeEnabled[]="is-plugin-mode-enabled:";
lenclud@4
    70
const char KMsgSetVfdText[]="set-vfd-text:";
StephaneLenclud@6
    71
const char KMsgQuit[]="quit:";
lenclud@4
    72
StephaneLenclud@6
    73
/**
StephaneLenclud@6
    74
Create inbound pipe to receive messages.
StephaneLenclud@6
    75
*/
StephaneLenclud@6
    76
bool CreateInboundPipe()
StephaneLenclud@6
    77
	{
StephaneLenclud@6
    78
	gPipeInbound = CreateNamedPipe(gPipeNameInbound, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE, PIPE_UNLIMITED_INSTANCES, BUFFER_SIZE, BUFFER_SIZE, 0, NULL);
StephaneLenclud@6
    79
StephaneLenclud@6
    80
	//could not create named pipe
StephaneLenclud@6
    81
    if (gPipeInbound==INVALID_HANDLE_VALUE)
StephaneLenclud@6
    82
        return false;
StephaneLenclud@6
    83
StephaneLenclud@6
    84
    //Will complete once a client connects
StephaneLenclud@6
    85
    int success = ConnectNamedPipe(gPipeInbound, NULL);
StephaneLenclud@6
    86
StephaneLenclud@6
    87
    //could not connect client
StephaneLenclud@6
    88
    if (success == 0)
StephaneLenclud@6
    89
        return false;
StephaneLenclud@6
    90
StephaneLenclud@6
    91
	return true;
StephaneLenclud@6
    92
	}
StephaneLenclud@6
    93
StephaneLenclud@6
    94
StephaneLenclud@6
    95
/**
StephaneLenclud@6
    96
Create outbound pipe to send message
StephaneLenclud@6
    97
*/
StephaneLenclud@6
    98
bool CreateOutboundPipe()
StephaneLenclud@6
    99
	{
StephaneLenclud@6
   100
	gPipeOutbound = CreateNamedPipe(gPipeNameOutbound, PIPE_ACCESS_OUTBOUND /*| FILE_FLAG_OVERLAPPED*/, PIPE_TYPE_BYTE, PIPE_UNLIMITED_INSTANCES, BUFFER_SIZE, BUFFER_SIZE, 0, NULL);
StephaneLenclud@6
   101
StephaneLenclud@6
   102
	//could not create named pipe
StephaneLenclud@6
   103
    if (gPipeOutbound==INVALID_HANDLE_VALUE)
StephaneLenclud@6
   104
        return false;
StephaneLenclud@6
   105
StephaneLenclud@6
   106
    //Will complete once a client connects
StephaneLenclud@6
   107
    int success = ConnectNamedPipe(gPipeOutbound, NULL);
StephaneLenclud@6
   108
StephaneLenclud@6
   109
    //could not connect client
StephaneLenclud@6
   110
    if (success == 0)
StephaneLenclud@6
   111
        return false;
StephaneLenclud@6
   112
StephaneLenclud@6
   113
	return true;
StephaneLenclud@6
   114
	}
lenclud@2
   115
lenclud@2
   116
/**
lenclud@2
   117
Send a message to our server.
lenclud@2
   118
*/
lenclud@2
   119
void SendMessageToServer(const char* aResponse)
lenclud@2
   120
	{
lenclud@2
   121
	DWORD cbWritten=0;
lenclud@3
   122
	char msg[512];
lenclud@3
   123
	sprintf(msg,"%s\n",aResponse);
lenclud@3
   124
	OutputDebugStringA(msg);
StephaneLenclud@6
   125
	WriteFile(gPipeOutbound, aResponse, strlen(aResponse), &cbWritten, NULL);
StephaneLenclud@6
   126
	if (strlen(aResponse)!=cbWritten)
StephaneLenclud@6
   127
		{
StephaneLenclud@6
   128
		int err = GetLastError();
StephaneLenclud@6
   129
		OutputDebugStringA("ERROR: WriteFile failure!\n");
StephaneLenclud@6
   130
		}
lenclud@2
   131
	}
lenclud@2
   132
lenclud@2
   133
StephaneLenclud@6
   134
lenclud@2
   135
/**
lenclud@2
   136
Handle messages from our server.
lenclud@2
   137
*/
lenclud@2
   138
void MessageReceived(const char* aMsg)
lenclud@2
   139
	{
lenclud@2
   140
	//
lenclud@3
   141
	if (strcmp(aMsg,KMsgInit)==0)
lenclud@2
   142
		{
lenclud@2
   143
		//IMON API call need to be done from window thread for some reason
lenclud@3
   144
		SendMessageToServer(KRspPending);
lenclud@3
   145
		PostMessage(gWnd,WM_IMON_INIT,0,0);
lenclud@3
   146
		}
lenclud@3
   147
	else if (strcmp(aMsg,KMsgUninit)==0)
lenclud@3
   148
		{
lenclud@3
   149
		//IMON API call need to be done from window thread for some reason
lenclud@3
   150
		SendMessageToServer(KRspPending);
lenclud@3
   151
		PostMessage(gWnd,WM_IMON_UNINIT,0,0);		
lenclud@3
   152
		}
lenclud@3
   153
	else if (strcmp(aMsg,KMsgIsPluginModeEnabled)==0)
lenclud@3
   154
		{
lenclud@3
   155
		//IMON API call need to be done from window thread for some reason
lenclud@3
   156
		SendMessageToServer(KRspPending);
lenclud@3
   157
		PostMessage(gWnd,WM_IMON_IS_PLUGIN_MODE_ENABLED,0,0);		
lenclud@3
   158
		}
lenclud@3
   159
	else if (strcmp(aMsg,KMsgIsInit)==0)
lenclud@3
   160
		{
lenclud@3
   161
		//IMON API call need to be done from window thread for some reason
lenclud@3
   162
		SendMessageToServer(KRspPending);
StephaneLenclud@6
   163
		PostMessage(gWnd,WM_IMON_IS_INIT,0,0);
StephaneLenclud@6
   164
		}
StephaneLenclud@6
   165
	else if (strcmp(aMsg,KMsgQuit)==0)
StephaneLenclud@6
   166
		{
StephaneLenclud@6
   167
		//gQuit=TRUE;
StephaneLenclud@6
   168
		SendMessageToServer(KRspDone);
StephaneLenclud@6
   169
		DestroyWindow(gWnd);
lenclud@3
   170
		}
lenclud@4
   171
	else if (strstr(aMsg,KMsgSetVfdText)==aMsg)
lenclud@4
   172
		{
lenclud@4
   173
		int textLen=strlen(aMsg)-strlen(KMsgSetVfdText);
lenclud@4
   174
		strncpy(gTextFirstLine,aMsg+strlen(KMsgSetVfdText),textLen);
lenclud@4
   175
		gTextFirstLine[textLen]='\0';
StephaneLenclud@7
   176
		//Check if we have a second line
StephaneLenclud@7
   177
		char* ptr=strchr(gTextFirstLine,'\n');
StephaneLenclud@7
   178
		if (ptr!=NULL)
StephaneLenclud@7
   179
			{
StephaneLenclud@7
   180
			*ptr='\0'; //Terminate our first line here
StephaneLenclud@7
   181
			ptr++;
StephaneLenclud@7
   182
			//Get our second line
StephaneLenclud@7
   183
			textLen=strlen(ptr);
StephaneLenclud@7
   184
			strncpy(gTextSecondLine,ptr,textLen);
StephaneLenclud@7
   185
			gTextSecondLine[textLen]='\0';
StephaneLenclud@7
   186
			}
StephaneLenclud@7
   187
		else
StephaneLenclud@7
   188
			{
StephaneLenclud@7
   189
			//No second line specified
StephaneLenclud@7
   190
			gTextSecondLine[0]='\0';
StephaneLenclud@7
   191
			gTextSecondLine16[0]='\0';
StephaneLenclud@7
   192
			}
StephaneLenclud@7
   193
StephaneLenclud@7
   194
		//Convert first line
lenclud@4
   195
		OutputDebugStringA(gTextFirstLine);
lenclud@4
   196
		int convertedChars = MultiByteToWideChar(CP_UTF8, 0, gTextFirstLine, -1, gTextFirstLine16, sizeof(gTextFirstLine16));
lenclud@4
   197
		OutputDebugString(gTextFirstLine16);
StephaneLenclud@7
   198
		//Convert second line
StephaneLenclud@7
   199
		OutputDebugStringA(gTextSecondLine);
StephaneLenclud@7
   200
		convertedChars = MultiByteToWideChar(CP_UTF8, 0, gTextSecondLine, -1, gTextSecondLine16, sizeof(gTextSecondLine16));
StephaneLenclud@7
   201
		OutputDebugString(gTextSecondLine16);
StephaneLenclud@7
   202
lenclud@4
   203
		//IMON API call need to be done from window thread for some reason
lenclud@4
   204
		SendMessageToServer(KRspPending);
StephaneLenclud@5
   205
		if (!m_IsInit)
StephaneLenclud@5
   206
			{
StephaneLenclud@5
   207
			PostMessage(gWnd,WM_IMON_INIT,0,0);		
StephaneLenclud@5
   208
			}
lenclud@4
   209
		PostMessage(gWnd,WM_IMON_DISPLAY_SET_VFD_TEXT,0,0);		
lenclud@4
   210
		}
lenclud@3
   211
	else
lenclud@3
   212
		{		
lenclud@3
   213
		SendMessageToServer(KRspUnknown);
lenclud@3
   214
		}
lenclud@3
   215
	}
lenclud@3
   216
lenclud@3
   217
lenclud@3
   218
lenclud@3
   219
/**
lenclud@3
   220
Connect our named pipes from here.
lenclud@3
   221
First connect our read pipe then our write pipe.
lenclud@3
   222
Then notify our server we are connected.
lenclud@3
   223
Then keep on waiting for incoming messages.
lenclud@3
   224
*/
lenclud@3
   225
DWORD WINAPI ThreadReceiver( LPVOID lpParam )
lenclud@3
   226
	{
StephaneLenclud@6
   227
StephaneLenclud@6
   228
	/*
lenclud@3
   229
	//Keep on trying to connect on our read pipe
StephaneLenclud@6
   230
	while (gPipeInbound==INVALID_HANDLE_VALUE && !gQuit)
lenclud@3
   231
		{
lenclud@3
   232
		OutputDebugStringA("Trying to connect...\n");
StephaneLenclud@6
   233
		gPipeInbound=CreateFile(gPipeNameInbound,	GENERIC_READ ,0,NULL,OPEN_EXISTING,0,NULL);
lenclud@3
   234
		Sleep(1000);
lenclud@3
   235
		}
lenclud@3
   236
	
lenclud@3
   237
	OutputDebugStringA("Read pipe open!\n");
lenclud@3
   238
	//Now try connecting on our write pipe
StephaneLenclud@6
   239
	gPipeOutbound=CreateFile(gPipeNameOutbound,	GENERIC_WRITE ,0,NULL,OPEN_EXISTING,0,NULL);
StephaneLenclud@6
   240
	if (gPipeOutbound==INVALID_HANDLE_VALUE)
lenclud@3
   241
		{
StephaneLenclud@5
   242
		int err=GetLastError();
lenclud@3
   243
		OutputDebugStringA("ERROR: Write pipe failure!\n");
StephaneLenclud@5
   244
		//gQuit=TRUE;
lenclud@2
   245
		}
lenclud@2
   246
	else
lenclud@2
   247
		{
lenclud@3
   248
		OutputDebugStringA("Write pipe opened.\n");
lenclud@3
   249
		OutputDebugStringA("Connected.\n");
lenclud@3
   250
		SendMessageToServer(KRspOpen);
lenclud@2
   251
		}
lenclud@3
   252
	//
StephaneLenclud@6
   253
	*/
StephaneLenclud@6
   254
StephaneLenclud@6
   255
	
StephaneLenclud@6
   256
	CreateOutboundPipe();
StephaneLenclud@6
   257
	CreateInboundPipe();
sl@0
   258
sl@0
   259
	while(!gQuit)
sl@0
   260
		{
sl@0
   261
		DWORD cbRead;
StephaneLenclud@6
   262
		BOOL success=ReadFile(gPipeInbound,gBufferReceiver,sizeof(gBufferReceiver),&cbRead, NULL); 
sl@0
   263
		if(success)
lenclud@3
   264
			{
sl@0
   265
			gBufferReceiver[cbRead]='\0';
sl@0
   266
			OutputDebugStringA(gBufferReceiver);
lenclud@2
   267
			MessageReceived(gBufferReceiver);
sl@0
   268
			}
lenclud@3
   269
		//if (!success && GetLastError() != ERROR_MORE_DATA) 
lenclud@3
   270
		//	{
lenclud@3
   271
		//	OutputDebugStringA("Can't Read\n");
lenclud@3
   272
		//	}
lenclud@3
   273
		//
lenclud@3
   274
		//Sleep(500);
lenclud@3
   275
lenclud@3
   276
		if (!success)
sl@0
   277
			{
lenclud@3
   278
			gQuit=TRUE;
sl@0
   279
			}
sl@0
   280
		}
sl@0
   281
lenclud@3
   282
	//Try tell our server
lenclud@3
   283
	SendMessageToServer(KRspClose);
lenclud@3
   284
	//Close our pipes
StephaneLenclud@6
   285
	DisconnectNamedPipe(gPipeInbound);
StephaneLenclud@6
   286
	CloseHandle(gPipeInbound);
StephaneLenclud@6
   287
	gPipeInbound=INVALID_HANDLE_VALUE;
StephaneLenclud@6
   288
	DisconnectNamedPipe(gPipeOutbound);
StephaneLenclud@6
   289
	CloseHandle(gPipeOutbound);	
StephaneLenclud@6
   290
	gPipeOutbound=INVALID_HANDLE_VALUE;
lenclud@3
   291
	//Quit our application
lenclud@3
   292
	PostMessage(gWnd,WM_CLOSE,0,0);
lenclud@3
   293
		
sl@0
   294
	return 0;
sl@0
   295
	}
sl@0
   296
sl@0
   297
sl@0
   298
sl@0
   299
/**
sl@0
   300
*/
sl@0
   301
int APIENTRY _tWinMain(HINSTANCE hInstance,
sl@0
   302
                     HINSTANCE hPrevInstance,
sl@0
   303
                     LPTSTR    lpCmdLine,
sl@0
   304
                     int       nCmdShow)
sl@0
   305
{
sl@0
   306
	UNREFERENCED_PARAMETER(hPrevInstance);
sl@0
   307
	UNREFERENCED_PARAMETER(lpCmdLine);
sl@0
   308
lenclud@4
   309
	gTextFirstLine[0]='\0';
lenclud@4
   310
	gTextSecondLine[0]='\0';
lenclud@4
   311
	gTextFirstLine16[0]='\0';
lenclud@4
   312
	gTextSecondLine16[0]='\0';
lenclud@4
   313
lenclud@4
   314
sl@0
   315
	// TODO: Place code here.
sl@0
   316
	MSG msg;
sl@0
   317
	HACCEL hAccelTable;
sl@0
   318
sl@0
   319
	// Initialize global strings
sl@0
   320
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
sl@0
   321
	LoadString(hInstance, IDC_SOUNDGRAPHACCESS, szWindowClass, MAX_LOADSTRING);
sl@0
   322
	MyRegisterClass(hInstance);
sl@0
   323
sl@0
   324
	// Perform application initialization:
sl@0
   325
	if (!InitInstance (hInstance, /*SW_HIDE*/ nCmdShow))
sl@0
   326
		{
sl@0
   327
		return FALSE;
sl@0
   328
		}
sl@0
   329
sl@0
   330
sl@0
   331
sl@0
   332
	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS));
sl@0
   333
sl@0
   334
	// Main message loop:
sl@0
   335
	while (GetMessage(&msg, NULL, 0, 0))
sl@0
   336
		{
sl@0
   337
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
sl@0
   338
			{
sl@0
   339
			TranslateMessage(&msg);
sl@0
   340
			DispatchMessage(&msg);
sl@0
   341
			}
sl@0
   342
		}
sl@0
   343
sl@0
   344
	return (int) msg.wParam;
sl@0
   345
	}
sl@0
   346
lenclud@2
   347
/**
lenclud@2
   348
*/
sl@0
   349
void DisplayPluginMessage(UINT uErrCode, BOOL bError)
sl@0
   350
	{
lenclud@2
   351
	char* strErrMsg = "";
sl@0
   352
sl@0
   353
	if(bError)
sl@0
   354
		{
sl@0
   355
		switch(uErrCode)
sl@0
   356
			{
lenclud@2
   357
			case DSPN_ERR_IN_USED:			strErrMsg = ("Display Plug-in is Already Used by Other Application.");		break;
lenclud@2
   358
			case DSPN_ERR_HW_DISCONNECTED:	strErrMsg = ("iMON HW is Not Connected.");									break;
lenclud@2
   359
			case DSPN_ERR_NOT_SUPPORTED_HW:	strErrMsg = ("The Connected iMON HW doesn't Support Display Plug-in.");		break;
lenclud@3
   360
			case DSPN_ERR_PLUGIN_DISABLED:	strErrMsg = ("Display Plug-in Mode Option is Disabled.");					break;
lenclud@2
   361
			case DSPN_ERR_IMON_NO_REPLY:	strErrMsg = ("The Latest iMON is Not Installed or iMON Not Running.");		break;
lenclud@3
   362
			case DSPN_ERR_UNKNOWN:			strErrMsg = ("Unknown Failure.");											break;
sl@0
   363
			}
sl@0
   364
		}
sl@0
   365
	else
sl@0
   366
		{
sl@0
   367
		switch(uErrCode)
sl@0
   368
			{
lenclud@3
   369
			case DSPNM_PLUGIN_SUCCEED:		strErrMsg = ("Plug-in Mode Inited Successfully.");			break;
lenclud@2
   370
			case DSPNM_IMON_RESTARTED:		strErrMsg = ("iMON Started and Plug-in Mode Inited.");		break;
lenclud@2
   371
			case DSPNM_HW_CONNECTED:		strErrMsg = ("iMON HW Connected and Plug-in Mode Inited.");	break;
sl@0
   372
			}
StephaneLenclud@5
   373
StephaneLenclud@5
   374
		m_IsInit=TRUE;
sl@0
   375
		}
sl@0
   376
	//
lenclud@2
   377
	OutputDebugStringA(strErrMsg);
lenclud@3
   378
lenclud@3
   379
	char msg[256];
lenclud@3
   380
	if (bError)
lenclud@3
   381
		{
lenclud@3
   382
		sprintf(msg,"error:%s",strErrMsg);
lenclud@3
   383
		}
lenclud@3
   384
	else
lenclud@3
   385
		{
lenclud@3
   386
		sprintf(msg,"done:%s",strErrMsg);	
lenclud@3
   387
		}	
lenclud@3
   388
	SendMessageToServer(msg);
sl@0
   389
	}
sl@0
   390
sl@0
   391
sl@0
   392
sl@0
   393
//
sl@0
   394
//  FUNCTION: MyRegisterClass()
sl@0
   395
//
sl@0
   396
//  PURPOSE: Registers the window class.
sl@0
   397
//
sl@0
   398
//  COMMENTS:
sl@0
   399
//
sl@0
   400
//    This function and its usage are only necessary if you want this code
sl@0
   401
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
sl@0
   402
//    function that was added to Windows 95. It is important to call this function
sl@0
   403
//    so that the application will get 'well formed' small icons associated
sl@0
   404
//    with it.
sl@0
   405
//
sl@0
   406
ATOM MyRegisterClass(HINSTANCE hInstance)
sl@0
   407
{
sl@0
   408
	WNDCLASSEX wcex;
sl@0
   409
sl@0
   410
	wcex.cbSize = sizeof(WNDCLASSEX);
sl@0
   411
sl@0
   412
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
sl@0
   413
	wcex.lpfnWndProc	= WndProc;
sl@0
   414
	wcex.cbClsExtra		= 0;
sl@0
   415
	wcex.cbWndExtra		= 0;
sl@0
   416
	wcex.hInstance		= hInstance;
sl@0
   417
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SOUNDGRAPHACCESS));
sl@0
   418
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
sl@0
   419
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
sl@0
   420
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS);
sl@0
   421
	wcex.lpszClassName	= szWindowClass;
sl@0
   422
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
sl@0
   423
sl@0
   424
	return RegisterClassEx(&wcex);
sl@0
   425
}
sl@0
   426
sl@0
   427
//
sl@0
   428
//   FUNCTION: InitInstance(HINSTANCE, int)
sl@0
   429
//
sl@0
   430
//   PURPOSE: Saves instance handle and creates main window
sl@0
   431
//
sl@0
   432
//   COMMENTS:
sl@0
   433
//
sl@0
   434
//        In this function, we save the instance handle in a global variable and
sl@0
   435
//        create and display the main program window.
sl@0
   436
//
sl@0
   437
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
sl@0
   438
{
lenclud@2
   439
   
sl@0
   440
sl@0
   441
   hInst = hInstance; // Store instance handle in our global variable
sl@0
   442
lenclud@2
   443
   gWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
sl@0
   444
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
sl@0
   445
lenclud@2
   446
   if (!gWnd)
sl@0
   447
   {
sl@0
   448
      return FALSE;
sl@0
   449
   }
sl@0
   450
lenclud@2
   451
   ShowWindow(gWnd, nCmdShow);
lenclud@2
   452
   UpdateWindow(gWnd);
sl@0
   453
sl@0
   454
   return TRUE;
sl@0
   455
}
sl@0
   456
sl@0
   457
//
sl@0
   458
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
sl@0
   459
//
sl@0
   460
//  PURPOSE:  Processes messages for the main window.
sl@0
   461
//
sl@0
   462
//  WM_COMMAND	- process the application menu
sl@0
   463
//  WM_PAINT	- Paint the main window
sl@0
   464
//  WM_DESTROY	- post a quit message and return
sl@0
   465
//
sl@0
   466
//
sl@0
   467
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
sl@0
   468
{
sl@0
   469
	int wmId, wmEvent;
sl@0
   470
	PAINTSTRUCT ps;
sl@0
   471
	HDC hdc;
sl@0
   472
sl@0
   473
	switch (message)
sl@0
   474
	{
sl@0
   475
	case WM_CREATE:
sl@0
   476
		//IMON_Display_Uninit();
sl@0
   477
		//IMON_Display_Init(hWnd, WM_DSP_PLUGIN_NOTIFY);
sl@0
   478
		gThreadReceiver = CreateThread( NULL, 0, ThreadReceiver, NULL/*data pointer*/, 0, NULL);
sl@0
   479
		break;
sl@0
   480
	case WM_COMMAND:
sl@0
   481
		wmId    = LOWORD(wParam);
sl@0
   482
		wmEvent = HIWORD(wParam);
sl@0
   483
		// Parse the menu selections:
sl@0
   484
		switch (wmId)
sl@0
   485
		{
sl@0
   486
		case IDM_ABOUT:
sl@0
   487
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
sl@0
   488
			break;
sl@0
   489
		case IDM_EXIT:
sl@0
   490
			DestroyWindow(hWnd);
sl@0
   491
			break;
sl@0
   492
		default:
sl@0
   493
			return DefWindowProc(hWnd, message, wParam, lParam);
sl@0
   494
		}
sl@0
   495
		break;
sl@0
   496
	case WM_PAINT:
sl@0
   497
		hdc = BeginPaint(hWnd, &ps);
sl@0
   498
		// TODO: Add any drawing code here...
sl@0
   499
		EndPaint(hWnd, &ps);
sl@0
   500
		break;
sl@0
   501
	case WM_DESTROY:
sl@0
   502
		gQuit=TRUE;
sl@0
   503
		//To complete write op
StephaneLenclud@6
   504
		if (gPipeOutbound!=INVALID_HANDLE_VALUE)
sl@0
   505
			{
StephaneLenclud@6
   506
			DisconnectNamedPipe(gPipeOutbound);
StephaneLenclud@6
   507
			CloseHandle(gPipeOutbound);
StephaneLenclud@6
   508
			gPipeOutbound=INVALID_HANDLE_VALUE;
sl@0
   509
			}
sl@0
   510
		//To complete read op
StephaneLenclud@6
   511
		if (gPipeInbound!=INVALID_HANDLE_VALUE)
sl@0
   512
			{
StephaneLenclud@6
   513
			DisconnectNamedPipe(gPipeInbound);
StephaneLenclud@6
   514
			CloseHandle(gPipeInbound);
StephaneLenclud@6
   515
			gPipeInbound=INVALID_HANDLE_VALUE;
sl@0
   516
			}
lenclud@3
   517
sl@0
   518
		WaitForSingleObject(gThreadReceiver,INFINITE);
sl@0
   519
		CloseHandle(gThreadReceiver);
sl@0
   520
		//IMON_Display_Uninit();
sl@0
   521
		PostQuitMessage(0);
sl@0
   522
		break;
lenclud@2
   523
lenclud@2
   524
	case WM_IMON_UNINIT:
StephaneLenclud@5
   525
		m_IsInit=FALSE;
lenclud@2
   526
		IMON_Display_Uninit();
lenclud@3
   527
		SendMessageToServer(KRspDone);
lenclud@2
   528
		break;
lenclud@2
   529
lenclud@2
   530
	case WM_IMON_INIT:
lenclud@2
   531
		IMON_Display_Uninit();
lenclud@2
   532
		IMON_Display_Init(hWnd, WM_DSP_PLUGIN_NOTIFY);
lenclud@2
   533
		break;
lenclud@3
   534
	//
lenclud@3
   535
	case WM_IMON_IS_PLUGIN_MODE_ENABLED:
lenclud@3
   536
		if (IMON_Display_IsPluginModeEnabled()==DSP_S_IN_PLUGIN_MODE)
lenclud@3
   537
			{
lenclud@3
   538
			SendMessageToServer(KRspTrue);
lenclud@3
   539
			}
lenclud@3
   540
		else
lenclud@3
   541
			{
lenclud@3
   542
			SendMessageToServer(KRspFalse);
lenclud@3
   543
			}
lenclud@3
   544
		break;
lenclud@3
   545
	//
lenclud@3
   546
	case WM_IMON_IS_INIT:
lenclud@3
   547
		if (IMON_Display_IsInited()==DSP_S_INITED)
lenclud@3
   548
			{
lenclud@3
   549
			SendMessageToServer(KRspTrue);
lenclud@3
   550
			}
lenclud@3
   551
		else
lenclud@3
   552
			{
lenclud@3
   553
			SendMessageToServer(KRspFalse);
lenclud@3
   554
			}
lenclud@3
   555
		break;
lenclud@4
   556
	//
lenclud@4
   557
	case WM_IMON_DISPLAY_SET_VFD_TEXT:
lenclud@4
   558
		if (DSP_SUCCEEDED==IMON_Display_SetVfdText(gTextFirstLine16,gTextSecondLine16))
lenclud@4
   559
			{
lenclud@4
   560
			SendMessageToServer(KRspDone);
lenclud@4
   561
			}
lenclud@4
   562
		else
lenclud@4
   563
			{
lenclud@4
   564
			SendMessageToServer(KRspError);
lenclud@4
   565
			}
lenclud@4
   566
		break;
lenclud@2
   567
sl@0
   568
	case WM_DSP_PLUGIN_NOTIFY:
sl@0
   569
		switch(wParam)
sl@0
   570
			{
sl@0
   571
			case DSPNM_PLUGIN_SUCCEED:
sl@0
   572
			case DSPNM_IMON_RESTARTED:
sl@0
   573
			case DSPNM_HW_CONNECTED:
sl@0
   574
				{
sl@0
   575
				//GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
sl@0
   576
				m_bVfdConnected = FALSE;
sl@0
   577
				m_bLcdConnected = FALSE;
sl@0
   578
				if((lParam & DSPN_DSP_VFD) == DSPN_DSP_VFD)		m_bVfdConnected = TRUE;
sl@0
   579
				if((lParam & DSPN_DSP_LCD) == DSPN_DSP_LCD)		m_bLcdConnected = TRUE;
sl@0
   580
				//UpdateControlUI();
sl@0
   581
sl@0
   582
				DisplayPluginMessage(wParam, FALSE);
sl@0
   583
				}
sl@0
   584
				break;
sl@0
   585
sl@0
   586
			case DSPNM_PLUGIN_FAILED:
sl@0
   587
			case DSPNM_HW_DISCONNECTED:
sl@0
   588
			case DSPNM_IMON_CLOSED:
sl@0
   589
				{
sl@0
   590
				//GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
sl@0
   591
				m_bVfdConnected = FALSE;
sl@0
   592
				m_bLcdConnected = FALSE;
sl@0
   593
				//UpdateControlUI();
sl@0
   594
sl@0
   595
				DisplayPluginMessage(lParam, TRUE);
sl@0
   596
				}
sl@0
   597
				break;
sl@0
   598
sl@0
   599
			case DSPNM_LCD_TEXT_SCROLL_DONE:
sl@0
   600
				{
sl@0
   601
				//TRACE(_T("LCD Text Scroll Finished.\n"));
sl@0
   602
				}
sl@0
   603
				break;
sl@0
   604
			}
sl@0
   605
		return 0;
sl@0
   606
		break;
sl@0
   607
	default:
sl@0
   608
		return DefWindowProc(hWnd, message, wParam, lParam);
sl@0
   609
	}
sl@0
   610
	return 0;
sl@0
   611
}
sl@0
   612
sl@0
   613
// Message handler for about box.
sl@0
   614
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
sl@0
   615
{
sl@0
   616
	UNREFERENCED_PARAMETER(lParam);
sl@0
   617
	switch (message)
sl@0
   618
	{
sl@0
   619
	case WM_INITDIALOG:
sl@0
   620
		return (INT_PTR)TRUE;
sl@0
   621
sl@0
   622
	case WM_COMMAND:
sl@0
   623
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
sl@0
   624
		{
sl@0
   625
			EndDialog(hDlg, LOWORD(wParam));
sl@0
   626
			return (INT_PTR)TRUE;
sl@0
   627
		}
sl@0
   628
		break;
sl@0
   629
	}
sl@0
   630
	return (INT_PTR)FALSE;
sl@0
   631
}
sl@0
   632