SoundGraphAccess.cpp
author sl
Sat, 15 Mar 2014 13:03:01 +0100
changeset 12 2f70b4447d70
parent 8 3031cd3ebd1e
permissions -rw-r--r--
Upgrade solution from Express to VS2012
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);
StephaneLenclud@8
   196
		//int convertedChars = MultiByteToWideChar(CP_UTF8, 0, gTextFirstLine, -1, gTextFirstLine16, sizeof(gTextFirstLine16));
StephaneLenclud@8
   197
		for (int i=0;i<=strlen(gTextFirstLine);i++)
StephaneLenclud@8
   198
			{
StephaneLenclud@8
   199
			unsigned char myChar=(unsigned char)gTextFirstLine[i];
StephaneLenclud@8
   200
			gTextFirstLine16[i]=myChar;
StephaneLenclud@8
   201
			}
StephaneLenclud@8
   202
		OutputDebugStringW(gTextFirstLine16);
StephaneLenclud@7
   203
		//Convert second line
StephaneLenclud@7
   204
		OutputDebugStringA(gTextSecondLine);
StephaneLenclud@8
   205
		//convertedChars = MultiByteToWideChar(CP_UTF8, 0, gTextSecondLine, -1, gTextSecondLine16, sizeof(gTextSecondLine16));
StephaneLenclud@8
   206
		for (int i=0;i<=strlen(gTextSecondLine);i++)
StephaneLenclud@8
   207
			{
StephaneLenclud@8
   208
			unsigned char myChar=(unsigned char)gTextSecondLine[i];
StephaneLenclud@8
   209
			gTextSecondLine16[i]=myChar;
StephaneLenclud@8
   210
			}
StephaneLenclud@8
   211
StephaneLenclud@8
   212
		OutputDebugStringW(gTextSecondLine16);
StephaneLenclud@7
   213
lenclud@4
   214
		//IMON API call need to be done from window thread for some reason
lenclud@4
   215
		SendMessageToServer(KRspPending);
StephaneLenclud@5
   216
		if (!m_IsInit)
StephaneLenclud@5
   217
			{
StephaneLenclud@5
   218
			PostMessage(gWnd,WM_IMON_INIT,0,0);		
StephaneLenclud@5
   219
			}
lenclud@4
   220
		PostMessage(gWnd,WM_IMON_DISPLAY_SET_VFD_TEXT,0,0);		
lenclud@4
   221
		}
lenclud@3
   222
	else
lenclud@3
   223
		{		
lenclud@3
   224
		SendMessageToServer(KRspUnknown);
lenclud@3
   225
		}
lenclud@3
   226
	}
lenclud@3
   227
lenclud@3
   228
lenclud@3
   229
lenclud@3
   230
/**
lenclud@3
   231
Connect our named pipes from here.
lenclud@3
   232
First connect our read pipe then our write pipe.
lenclud@3
   233
Then notify our server we are connected.
lenclud@3
   234
Then keep on waiting for incoming messages.
lenclud@3
   235
*/
lenclud@3
   236
DWORD WINAPI ThreadReceiver( LPVOID lpParam )
lenclud@3
   237
	{
StephaneLenclud@6
   238
StephaneLenclud@6
   239
	/*
lenclud@3
   240
	//Keep on trying to connect on our read pipe
StephaneLenclud@6
   241
	while (gPipeInbound==INVALID_HANDLE_VALUE && !gQuit)
lenclud@3
   242
		{
lenclud@3
   243
		OutputDebugStringA("Trying to connect...\n");
StephaneLenclud@6
   244
		gPipeInbound=CreateFile(gPipeNameInbound,	GENERIC_READ ,0,NULL,OPEN_EXISTING,0,NULL);
lenclud@3
   245
		Sleep(1000);
lenclud@3
   246
		}
lenclud@3
   247
	
lenclud@3
   248
	OutputDebugStringA("Read pipe open!\n");
lenclud@3
   249
	//Now try connecting on our write pipe
StephaneLenclud@6
   250
	gPipeOutbound=CreateFile(gPipeNameOutbound,	GENERIC_WRITE ,0,NULL,OPEN_EXISTING,0,NULL);
StephaneLenclud@6
   251
	if (gPipeOutbound==INVALID_HANDLE_VALUE)
lenclud@3
   252
		{
StephaneLenclud@5
   253
		int err=GetLastError();
lenclud@3
   254
		OutputDebugStringA("ERROR: Write pipe failure!\n");
StephaneLenclud@5
   255
		//gQuit=TRUE;
lenclud@2
   256
		}
lenclud@2
   257
	else
lenclud@2
   258
		{
lenclud@3
   259
		OutputDebugStringA("Write pipe opened.\n");
lenclud@3
   260
		OutputDebugStringA("Connected.\n");
lenclud@3
   261
		SendMessageToServer(KRspOpen);
lenclud@2
   262
		}
lenclud@3
   263
	//
StephaneLenclud@6
   264
	*/
StephaneLenclud@6
   265
StephaneLenclud@6
   266
	
StephaneLenclud@6
   267
	CreateOutboundPipe();
StephaneLenclud@6
   268
	CreateInboundPipe();
sl@0
   269
sl@0
   270
	while(!gQuit)
sl@0
   271
		{
sl@0
   272
		DWORD cbRead;
StephaneLenclud@6
   273
		BOOL success=ReadFile(gPipeInbound,gBufferReceiver,sizeof(gBufferReceiver),&cbRead, NULL); 
sl@0
   274
		if(success)
lenclud@3
   275
			{
sl@0
   276
			gBufferReceiver[cbRead]='\0';
sl@0
   277
			OutputDebugStringA(gBufferReceiver);
lenclud@2
   278
			MessageReceived(gBufferReceiver);
sl@0
   279
			}
lenclud@3
   280
		//if (!success && GetLastError() != ERROR_MORE_DATA) 
lenclud@3
   281
		//	{
lenclud@3
   282
		//	OutputDebugStringA("Can't Read\n");
lenclud@3
   283
		//	}
lenclud@3
   284
		//
lenclud@3
   285
		//Sleep(500);
lenclud@3
   286
lenclud@3
   287
		if (!success)
sl@0
   288
			{
lenclud@3
   289
			gQuit=TRUE;
sl@0
   290
			}
sl@0
   291
		}
sl@0
   292
lenclud@3
   293
	//Try tell our server
lenclud@3
   294
	SendMessageToServer(KRspClose);
lenclud@3
   295
	//Close our pipes
StephaneLenclud@6
   296
	DisconnectNamedPipe(gPipeInbound);
StephaneLenclud@6
   297
	CloseHandle(gPipeInbound);
StephaneLenclud@6
   298
	gPipeInbound=INVALID_HANDLE_VALUE;
StephaneLenclud@6
   299
	DisconnectNamedPipe(gPipeOutbound);
StephaneLenclud@6
   300
	CloseHandle(gPipeOutbound);	
StephaneLenclud@6
   301
	gPipeOutbound=INVALID_HANDLE_VALUE;
lenclud@3
   302
	//Quit our application
lenclud@3
   303
	PostMessage(gWnd,WM_CLOSE,0,0);
lenclud@3
   304
		
sl@0
   305
	return 0;
sl@0
   306
	}
sl@0
   307
sl@0
   308
sl@0
   309
sl@0
   310
/**
sl@0
   311
*/
sl@0
   312
int APIENTRY _tWinMain(HINSTANCE hInstance,
sl@0
   313
                     HINSTANCE hPrevInstance,
sl@0
   314
                     LPTSTR    lpCmdLine,
sl@0
   315
                     int       nCmdShow)
sl@0
   316
{
sl@0
   317
	UNREFERENCED_PARAMETER(hPrevInstance);
sl@0
   318
	UNREFERENCED_PARAMETER(lpCmdLine);
sl@0
   319
lenclud@4
   320
	gTextFirstLine[0]='\0';
lenclud@4
   321
	gTextSecondLine[0]='\0';
lenclud@4
   322
	gTextFirstLine16[0]='\0';
lenclud@4
   323
	gTextSecondLine16[0]='\0';
lenclud@4
   324
lenclud@4
   325
sl@0
   326
	// TODO: Place code here.
sl@0
   327
	MSG msg;
sl@0
   328
	HACCEL hAccelTable;
sl@0
   329
sl@0
   330
	// Initialize global strings
sl@0
   331
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
sl@0
   332
	LoadString(hInstance, IDC_SOUNDGRAPHACCESS, szWindowClass, MAX_LOADSTRING);
sl@0
   333
	MyRegisterClass(hInstance);
sl@0
   334
sl@0
   335
	// Perform application initialization:
sl@0
   336
	if (!InitInstance (hInstance, /*SW_HIDE*/ nCmdShow))
sl@0
   337
		{
sl@0
   338
		return FALSE;
sl@0
   339
		}
sl@0
   340
sl@0
   341
sl@0
   342
sl@0
   343
	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS));
sl@0
   344
sl@0
   345
	// Main message loop:
sl@0
   346
	while (GetMessage(&msg, NULL, 0, 0))
sl@0
   347
		{
sl@0
   348
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
sl@0
   349
			{
sl@0
   350
			TranslateMessage(&msg);
sl@0
   351
			DispatchMessage(&msg);
sl@0
   352
			}
sl@0
   353
		}
sl@0
   354
sl@0
   355
	return (int) msg.wParam;
sl@0
   356
	}
sl@0
   357
lenclud@2
   358
/**
lenclud@2
   359
*/
sl@0
   360
void DisplayPluginMessage(UINT uErrCode, BOOL bError)
sl@0
   361
	{
lenclud@2
   362
	char* strErrMsg = "";
sl@0
   363
sl@0
   364
	if(bError)
sl@0
   365
		{
sl@0
   366
		switch(uErrCode)
sl@0
   367
			{
lenclud@2
   368
			case DSPN_ERR_IN_USED:			strErrMsg = ("Display Plug-in is Already Used by Other Application.");		break;
lenclud@2
   369
			case DSPN_ERR_HW_DISCONNECTED:	strErrMsg = ("iMON HW is Not Connected.");									break;
lenclud@2
   370
			case DSPN_ERR_NOT_SUPPORTED_HW:	strErrMsg = ("The Connected iMON HW doesn't Support Display Plug-in.");		break;
lenclud@3
   371
			case DSPN_ERR_PLUGIN_DISABLED:	strErrMsg = ("Display Plug-in Mode Option is Disabled.");					break;
lenclud@2
   372
			case DSPN_ERR_IMON_NO_REPLY:	strErrMsg = ("The Latest iMON is Not Installed or iMON Not Running.");		break;
lenclud@3
   373
			case DSPN_ERR_UNKNOWN:			strErrMsg = ("Unknown Failure.");											break;
sl@0
   374
			}
sl@0
   375
		}
sl@0
   376
	else
sl@0
   377
		{
sl@0
   378
		switch(uErrCode)
sl@0
   379
			{
lenclud@3
   380
			case DSPNM_PLUGIN_SUCCEED:		strErrMsg = ("Plug-in Mode Inited Successfully.");			break;
lenclud@2
   381
			case DSPNM_IMON_RESTARTED:		strErrMsg = ("iMON Started and Plug-in Mode Inited.");		break;
lenclud@2
   382
			case DSPNM_HW_CONNECTED:		strErrMsg = ("iMON HW Connected and Plug-in Mode Inited.");	break;
sl@0
   383
			}
StephaneLenclud@5
   384
StephaneLenclud@5
   385
		m_IsInit=TRUE;
sl@0
   386
		}
sl@0
   387
	//
lenclud@2
   388
	OutputDebugStringA(strErrMsg);
lenclud@3
   389
lenclud@3
   390
	char msg[256];
lenclud@3
   391
	if (bError)
lenclud@3
   392
		{
lenclud@3
   393
		sprintf(msg,"error:%s",strErrMsg);
lenclud@3
   394
		}
lenclud@3
   395
	else
lenclud@3
   396
		{
lenclud@3
   397
		sprintf(msg,"done:%s",strErrMsg);	
lenclud@3
   398
		}	
lenclud@3
   399
	SendMessageToServer(msg);
sl@0
   400
	}
sl@0
   401
sl@0
   402
sl@0
   403
sl@0
   404
//
sl@0
   405
//  FUNCTION: MyRegisterClass()
sl@0
   406
//
sl@0
   407
//  PURPOSE: Registers the window class.
sl@0
   408
//
sl@0
   409
//  COMMENTS:
sl@0
   410
//
sl@0
   411
//    This function and its usage are only necessary if you want this code
sl@0
   412
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
sl@0
   413
//    function that was added to Windows 95. It is important to call this function
sl@0
   414
//    so that the application will get 'well formed' small icons associated
sl@0
   415
//    with it.
sl@0
   416
//
sl@0
   417
ATOM MyRegisterClass(HINSTANCE hInstance)
sl@0
   418
{
sl@0
   419
	WNDCLASSEX wcex;
sl@0
   420
sl@0
   421
	wcex.cbSize = sizeof(WNDCLASSEX);
sl@0
   422
sl@0
   423
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
sl@0
   424
	wcex.lpfnWndProc	= WndProc;
sl@0
   425
	wcex.cbClsExtra		= 0;
sl@0
   426
	wcex.cbWndExtra		= 0;
sl@0
   427
	wcex.hInstance		= hInstance;
sl@0
   428
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SOUNDGRAPHACCESS));
sl@0
   429
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
sl@0
   430
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
sl@0
   431
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS);
sl@0
   432
	wcex.lpszClassName	= szWindowClass;
sl@0
   433
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
sl@0
   434
sl@0
   435
	return RegisterClassEx(&wcex);
sl@0
   436
}
sl@0
   437
sl@0
   438
//
sl@0
   439
//   FUNCTION: InitInstance(HINSTANCE, int)
sl@0
   440
//
sl@0
   441
//   PURPOSE: Saves instance handle and creates main window
sl@0
   442
//
sl@0
   443
//   COMMENTS:
sl@0
   444
//
sl@0
   445
//        In this function, we save the instance handle in a global variable and
sl@0
   446
//        create and display the main program window.
sl@0
   447
//
sl@0
   448
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
sl@0
   449
{
lenclud@2
   450
   
sl@0
   451
sl@0
   452
   hInst = hInstance; // Store instance handle in our global variable
sl@0
   453
lenclud@2
   454
   gWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
StephaneLenclud@11
   455
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, HWND_MESSAGE /*Make it invisible*/, NULL, hInstance, NULL);
sl@0
   456
lenclud@2
   457
   if (!gWnd)
sl@0
   458
   {
sl@0
   459
      return FALSE;
sl@0
   460
   }
sl@0
   461
StephaneLenclud@11
   462
   //We want it invisble so just don't show it :)
StephaneLenclud@11
   463
   //ShowWindow(gWnd, nCmdShow);
StephaneLenclud@11
   464
lenclud@2
   465
   UpdateWindow(gWnd);
sl@0
   466
sl@0
   467
   return TRUE;
sl@0
   468
}
sl@0
   469
sl@0
   470
//
sl@0
   471
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
sl@0
   472
//
sl@0
   473
//  PURPOSE:  Processes messages for the main window.
sl@0
   474
//
sl@0
   475
//  WM_COMMAND	- process the application menu
sl@0
   476
//  WM_PAINT	- Paint the main window
sl@0
   477
//  WM_DESTROY	- post a quit message and return
sl@0
   478
//
sl@0
   479
//
sl@0
   480
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
sl@0
   481
{
sl@0
   482
	int wmId, wmEvent;
sl@0
   483
	PAINTSTRUCT ps;
sl@0
   484
	HDC hdc;
sl@0
   485
sl@0
   486
	switch (message)
sl@0
   487
	{
sl@0
   488
	case WM_CREATE:
sl@0
   489
		//IMON_Display_Uninit();
sl@0
   490
		//IMON_Display_Init(hWnd, WM_DSP_PLUGIN_NOTIFY);
sl@0
   491
		gThreadReceiver = CreateThread( NULL, 0, ThreadReceiver, NULL/*data pointer*/, 0, NULL);
sl@0
   492
		break;
sl@0
   493
	case WM_COMMAND:
sl@0
   494
		wmId    = LOWORD(wParam);
sl@0
   495
		wmEvent = HIWORD(wParam);
sl@0
   496
		// Parse the menu selections:
sl@0
   497
		switch (wmId)
sl@0
   498
		{
sl@0
   499
		case IDM_ABOUT:
sl@0
   500
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
sl@0
   501
			break;
sl@0
   502
		case IDM_EXIT:
sl@0
   503
			DestroyWindow(hWnd);
sl@0
   504
			break;
sl@0
   505
		default:
sl@0
   506
			return DefWindowProc(hWnd, message, wParam, lParam);
sl@0
   507
		}
sl@0
   508
		break;
sl@0
   509
	case WM_PAINT:
sl@0
   510
		hdc = BeginPaint(hWnd, &ps);
sl@0
   511
		// TODO: Add any drawing code here...
sl@0
   512
		EndPaint(hWnd, &ps);
sl@0
   513
		break;
sl@0
   514
	case WM_DESTROY:
sl@0
   515
		gQuit=TRUE;
sl@0
   516
		//To complete write op
StephaneLenclud@6
   517
		if (gPipeOutbound!=INVALID_HANDLE_VALUE)
sl@0
   518
			{
StephaneLenclud@6
   519
			DisconnectNamedPipe(gPipeOutbound);
StephaneLenclud@6
   520
			CloseHandle(gPipeOutbound);
StephaneLenclud@6
   521
			gPipeOutbound=INVALID_HANDLE_VALUE;
sl@0
   522
			}
sl@0
   523
		//To complete read op
StephaneLenclud@6
   524
		if (gPipeInbound!=INVALID_HANDLE_VALUE)
sl@0
   525
			{
StephaneLenclud@6
   526
			DisconnectNamedPipe(gPipeInbound);
StephaneLenclud@6
   527
			CloseHandle(gPipeInbound);
StephaneLenclud@6
   528
			gPipeInbound=INVALID_HANDLE_VALUE;
sl@0
   529
			}
lenclud@3
   530
sl@0
   531
		WaitForSingleObject(gThreadReceiver,INFINITE);
sl@0
   532
		CloseHandle(gThreadReceiver);
sl@0
   533
		//IMON_Display_Uninit();
sl@0
   534
		PostQuitMessage(0);
sl@0
   535
		break;
lenclud@2
   536
lenclud@2
   537
	case WM_IMON_UNINIT:
StephaneLenclud@5
   538
		m_IsInit=FALSE;
lenclud@2
   539
		IMON_Display_Uninit();
lenclud@3
   540
		SendMessageToServer(KRspDone);
lenclud@2
   541
		break;
lenclud@2
   542
lenclud@2
   543
	case WM_IMON_INIT:
lenclud@2
   544
		IMON_Display_Uninit();
lenclud@2
   545
		IMON_Display_Init(hWnd, WM_DSP_PLUGIN_NOTIFY);
lenclud@2
   546
		break;
lenclud@3
   547
	//
lenclud@3
   548
	case WM_IMON_IS_PLUGIN_MODE_ENABLED:
lenclud@3
   549
		if (IMON_Display_IsPluginModeEnabled()==DSP_S_IN_PLUGIN_MODE)
lenclud@3
   550
			{
lenclud@3
   551
			SendMessageToServer(KRspTrue);
lenclud@3
   552
			}
lenclud@3
   553
		else
lenclud@3
   554
			{
lenclud@3
   555
			SendMessageToServer(KRspFalse);
lenclud@3
   556
			}
lenclud@3
   557
		break;
lenclud@3
   558
	//
lenclud@3
   559
	case WM_IMON_IS_INIT:
lenclud@3
   560
		if (IMON_Display_IsInited()==DSP_S_INITED)
lenclud@3
   561
			{
lenclud@3
   562
			SendMessageToServer(KRspTrue);
lenclud@3
   563
			}
lenclud@3
   564
		else
lenclud@3
   565
			{
lenclud@3
   566
			SendMessageToServer(KRspFalse);
lenclud@3
   567
			}
lenclud@3
   568
		break;
lenclud@4
   569
	//
lenclud@4
   570
	case WM_IMON_DISPLAY_SET_VFD_TEXT:
StephaneLenclud@8
   571
#ifdef _UNICODE
lenclud@4
   572
		if (DSP_SUCCEEDED==IMON_Display_SetVfdText(gTextFirstLine16,gTextSecondLine16))
StephaneLenclud@8
   573
#else
StephaneLenclud@8
   574
		if (DSP_SUCCEEDED==IMON_Display_SetVfdText(gTextFirstLine,gTextSecondLine))
StephaneLenclud@8
   575
#endif
lenclud@4
   576
			{
lenclud@4
   577
			SendMessageToServer(KRspDone);
lenclud@4
   578
			}
lenclud@4
   579
		else
lenclud@4
   580
			{
lenclud@4
   581
			SendMessageToServer(KRspError);
lenclud@4
   582
			}
lenclud@4
   583
		break;
lenclud@2
   584
sl@0
   585
	case WM_DSP_PLUGIN_NOTIFY:
sl@0
   586
		switch(wParam)
sl@0
   587
			{
sl@0
   588
			case DSPNM_PLUGIN_SUCCEED:
sl@0
   589
			case DSPNM_IMON_RESTARTED:
sl@0
   590
			case DSPNM_HW_CONNECTED:
sl@0
   591
				{
sl@0
   592
				//GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
sl@0
   593
				m_bVfdConnected = FALSE;
sl@0
   594
				m_bLcdConnected = FALSE;
sl@0
   595
				if((lParam & DSPN_DSP_VFD) == DSPN_DSP_VFD)		m_bVfdConnected = TRUE;
sl@0
   596
				if((lParam & DSPN_DSP_LCD) == DSPN_DSP_LCD)		m_bLcdConnected = TRUE;
sl@0
   597
				//UpdateControlUI();
sl@0
   598
sl@0
   599
				DisplayPluginMessage(wParam, FALSE);
sl@0
   600
				}
sl@0
   601
				break;
sl@0
   602
sl@0
   603
			case DSPNM_PLUGIN_FAILED:
sl@0
   604
			case DSPNM_HW_DISCONNECTED:
sl@0
   605
			case DSPNM_IMON_CLOSED:
sl@0
   606
				{
sl@0
   607
				//GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
sl@0
   608
				m_bVfdConnected = FALSE;
sl@0
   609
				m_bLcdConnected = FALSE;
sl@0
   610
				//UpdateControlUI();
sl@0
   611
sl@0
   612
				DisplayPluginMessage(lParam, TRUE);
sl@0
   613
				}
sl@0
   614
				break;
sl@0
   615
sl@0
   616
			case DSPNM_LCD_TEXT_SCROLL_DONE:
sl@0
   617
				{
sl@0
   618
				//TRACE(_T("LCD Text Scroll Finished.\n"));
sl@0
   619
				}
sl@0
   620
				break;
sl@0
   621
			}
sl@0
   622
		return 0;
sl@0
   623
		break;
sl@0
   624
	default:
sl@0
   625
		return DefWindowProc(hWnd, message, wParam, lParam);
sl@0
   626
	}
sl@0
   627
	return 0;
sl@0
   628
}
sl@0
   629
sl@0
   630
// Message handler for about box.
sl@0
   631
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
sl@0
   632
{
sl@0
   633
	UNREFERENCED_PARAMETER(lParam);
sl@0
   634
	switch (message)
sl@0
   635
	{
sl@0
   636
	case WM_INITDIALOG:
sl@0
   637
		return (INT_PTR)TRUE;
sl@0
   638
sl@0
   639
	case WM_COMMAND:
sl@0
   640
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
sl@0
   641
		{
sl@0
   642
			EndDialog(hDlg, LOWORD(wParam));
sl@0
   643
			return (INT_PTR)TRUE;
sl@0
   644
		}
sl@0
   645
		break;
sl@0
   646
	}
sl@0
   647
	return (INT_PTR)FALSE;
sl@0
   648
}
sl@0
   649