summaryrefslogtreecommitdiff
path: root/Bus/Ivy/IvySynchroWnd.cxx
blob: e2b2338886696eafcf8e73017cb6abff93c64b84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// SynchroWnd.cpp : implementation file
//

#include "stdafx.h"


#include "IvySynchroWnd.h"


#define WM_IVY_CB WM_USER + 1001


IvySynchroWnd* IvySynchronousCallback::m_synchro = NULL;
IvySynchroWnd* IvySynchroWnd::m_synchro = NULL;

/////////////////////////////////////////////////////////////////////////////
// IvySynchroWnd

IvySynchroWnd::IvySynchroWnd()
{
	m_hWnd = NULL;
	m_synchro = this;

	WNDCLASS wc; 
 
    // Fill in the window class structure with parameters 
    // that describe the main window. 
 
    wc.style = 0;                     // noredraw if size changes 
    wc.lpfnWndProc = WindowProc;			// points to window procedure 
    wc.cbClsExtra = 0;                // no extra class memory 
    wc.cbWndExtra = 0;                // no extra window memory 
    wc.hInstance = 0;							    // handle to instance 
    wc.hIcon = NULL;									// predefined app. icon 
    wc.hCursor = NULL;                // predefined arrow 
    wc.hbrBackground = 0;             // white background brush 
    wc.lpszMenuName =  NULL;					// no  menu 
    wc.lpszClassName = TEXT("IvySynchroClass");  // name of window class 
 
    // Register the window class. 
 
    if ( ! RegisterClass(&wc) )
			{
				TRACE("Warning: unable to create Ivy Synchro notify window!\n");
				//AfxThrowResourceException();
			}

		// Create the syncrho window. 
    m_hWnd  = CreateWindowEx(0, TEXT("IvySynchroClass"), TEXT("Ivy Synchro Notification Sink"),
				WS_OVERLAPPED, 0, 0, 0, 0, NULL , NULL, NULL, NULL);
		if (!m_hWnd)
			{
				TRACE("Warning: unable to create Ivy Synchro notify window!\n");
				//AfxThrowResourceException();
			}
	InitializeCriticalSection( &m_CritSection );

}
IvySynchroWnd::~IvySynchroWnd()
{
}


LRESULT CALLBACK IvySynchroWnd::WindowProc( HWND hwnd, UINT uMsg,  WPARAM wParam,  LPARAM lParam   )
{
	switch ( uMsg )
		{
		case WM_IVY_CB:
			m_synchro->OnIvyCB( wParam, lParam );
			break;
    // 
    // Process other messages. 
    // 

    default: 
        return DefWindowProc(hwnd, uMsg, wParam, lParam); 
    } 
    return 0; 
}




/////////////////////////////////////////////////////////////////////////////
// IvySynchroWnd message handlers


LRESULT IvySynchroWnd::OnIvyCB(WPARAM wParam, LPARAM lParam)
{
	IvySynchronousCallbackList::iterator iter;
	EnterCriticalSection( &m_CritSection );
	
	for ( iter = callbacklist.begin(); iter != callbacklist.end(); )
	{
	IvySynchronousCallback *param = *iter++;
	param->CallCallback();
	delete param;
	}
	callbacklist.clear();
	LeaveCriticalSection( &m_CritSection );	
	return 0L;
}
void IvySynchroWnd::PostIvyCB( IvySynchronousCallback *cb )
{
	EnterCriticalSection( &m_CritSection );
	if ( m_synchro->callbacklist.empty() )
		ASSERT(PostMessage(m_synchro->m_hWnd, WM_IVY_CB, 0, 0 ));
	m_synchro->callbacklist.push_back( cb );
	LeaveCriticalSection( &m_CritSection );	
}
//
// 
IvySynchronousMessageCallback::IvySynchronousMessageCallback( IvyMessageCallback *cb )
{
	target = cb;
}
IvySynchronousMessageCallback::~IvySynchronousMessageCallback()
{
	//delete target;
}
void IvySynchronousMessageCallback::OnMessage(IvyApplication *app, int argc, const char **argv )
{
	static int msg_count = 0;
	// duplicate on the Message Queue
	IvySynchronousMessageCallback *param = new IvySynchronousMessageCallback(target);
	param->app = app;
	param->argc = argc;
	param->argv = new char*[argc];
	for( int i = 0; i < argc ; i++ )
		{
		param->argv[i] = new char[ strlen( argv[i]) +1 ];
		strcpy( param->argv[i], argv[i]);
		}
//	TRACE( "IvySynchronousMessageCallback::OnMessage msg count %d\n",wParam);
	m_synchro->PostIvyCB( param );
		
}
void IvySynchronousMessageCallback::CallCallback()
{
	target->OnMessage( app, argc, (const char **) argv );
	for( int i = 0; i < argc ; i++ )
		delete argv[i];	
	delete  argv;	
}


IvySynchronousApplicationCallback::IvySynchronousApplicationCallback( IvyApplicationCallback *cb )
{
	target = cb;
}
void IvySynchronousApplicationCallback::OnApplicationConnected( IvyApplication *app)
{
	// duplicate on the Message Queue
	IvySynchronousApplicationCallback *param = new IvySynchronousApplicationCallback(target);
	param->type = CONNECTED_CB;
	param->app = app;
	m_synchro->PostIvyCB( param );
}

void IvySynchronousApplicationCallback::OnApplicationDisconnected(  IvyApplication *app)
{
	// duplicate on the Message Queue
	IvySynchronousApplicationCallback *param = new IvySynchronousApplicationCallback(target);
	param->type = DISCONNECTED_CB;
	param->app = app;
	m_synchro->PostIvyCB( param );
}
void IvySynchronousApplicationCallback::CallCallback()
{
	switch ( type )
	{
	case CONNECTED_CB:
		target->OnApplicationConnected( app );
		break;
	case DISCONNECTED_CB:
		target->OnApplicationDisconnected( app );
		break;
	}
}