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