summaryrefslogtreecommitdiff
path: root/Ivy/IvySynchroWnd.cxx
blob: 374538c90386dddbfca5305971581263a74dd308 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// SynchroWnd.cpp : implementation file
//

#include "IvyStdAfx.h"
#include "IvySynchroWnd.h"

#define WM_IVY_CB WM_USER + 1001

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()
{
}
void IvySynchroWnd::Init(bool synchronous)
{
	if ( synchronous && m_synchro == NULL )
		m_synchro = new IvySynchroWnd();
}
void IvySynchroWnd::Finish()
{
	if ( m_synchro == NULL )
		delete m_synchro;
}

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 )
{
	BOOL doPost = FALSE;
	EnterCriticalSection( &m_CritSection );
	doPost = callbacklist.empty();
	callbacklist.push_back( cb );
	LeaveCriticalSection( &m_CritSection );	
	if ( doPost )
		ASSERT(PostMessage(m_hWnd, WM_IVY_CB, 0, 0 ));	
	// give a change to process messages
	SwitchToThread();
	
}
BOOL CALLBACK MyQuitWindows( HWND hwnd, LPARAM lParam )
{
	BOOL *posted = (BOOL*)lParam;
	DWORD dwTheardId;
	DWORD pid;
	DWORD mypid = GetCurrentProcessId();
	dwTheardId = ::GetWindowThreadProcessId( hwnd,&pid);
	if ( pid == mypid )
	{
	// give a change to User for closing application safely, save file etc...
	*posted = PostMessage( hwnd, WM_CLOSE, 0,0 );
	}
	return TRUE;
}

void IvySynchroWnd::PostQuit()
{
	BOOL posted = FALSE;
	BOOL canceled = FALSE;
	EnumWindows(MyQuitWindows, (LPARAM)&posted );	
	// give a change to process messages
	if ( !SwitchToThread() )
	{
		Sleep( 200 );
	}
	if ( !posted )
	{
		PostQuitMessage( -1 );
		// give a change to process messages
		if ( !SwitchToThread() )
		{
			Sleep( 100 );
		}
		// ultimate close
		exit(-1);
	}
}

			
			//exit(-1);
//
// 
IvySynchronousMessageCallback::IvySynchronousMessageCallback( IvyMessageCallback *cb )
{
	target = cb;
	app = 0;
	argc = 0;
	argv = 0;
}
IvySynchronousMessageCallback::~IvySynchronousMessageCallback()
{
	//delete target;
	for( int i = 0; i < argc ; i++ )
		{
		delete argv[i];
		}
	if ( argv ) delete argv;
}
void IvySynchronousMessageCallback::OnMessage(IvyApplication *app, int argc, const char **argv )
{
	// 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++ )
		{
#ifdef UNDER_CE
		param->argv[i] = _strdup( argv[i]);
#else
		param->argv[i] = _strdup( argv[i]);
#endif
		}
#ifdef IVY_DEBUG
	TRACE( "IvySynchronousMessageCallback::OnMessage Push callback %s argc=%d\n",typeid( *target).name(), argc);
#endif
	IvySynchroWnd::m_synchro->PostIvyCB( param );
		
}
void IvySynchronousMessageCallback::CallCallback()
{
#ifdef IVY_DEBUG
	TRACE( "IvySynchronousMessageCallback::OnMessage Real callback %s argc=%d\n",typeid( *target).name(), argc);
#endif
	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;
	IvySynchroWnd::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;
	IvySynchroWnd::m_synchro->PostIvyCB( param );
}
void IvySynchronousApplicationCallback::CallCallback()
{
	if ( !target ) return;
	switch ( type )
	{
	case CONNECTED_CB:
		target->OnApplicationConnected( app );
		break;
	case DISCONNECTED_CB:
		target->OnApplicationDisconnected( app );
		break;
	}
}


IvySynchronousBindingCallback::IvySynchronousBindingCallback( IvyBindingCallback *cb )
{
	target = cb;
}
void IvySynchronousBindingCallback::OnAddBind( IvyApplication *app, int id, const char *regexp)
{
	// duplicate on the Message Queue
	IvySynchronousBindingCallback *param = new IvySynchronousBindingCallback(target);
	param->type = ADD_CB;
	param->app = app;
	param->id = id;
	param->regexp = _strdup(regexp);
	IvySynchroWnd::m_synchro->PostIvyCB( param );
}

void IvySynchronousBindingCallback::OnRemoveBind(  IvyApplication *app, int id, const char *regexp)
{
	// duplicate on the Message Queue
	IvySynchronousBindingCallback *param = new IvySynchronousBindingCallback(target);
	param->type = REMOVE_CB;
	param->app = app;
	param->id = id;
	param->regexp = _strdup(regexp);
	IvySynchroWnd::m_synchro->PostIvyCB( param );
}
void IvySynchronousBindingCallback::OnFilterBind(  IvyApplication *app, int id, const char *regexp)
{
	// duplicate on the Message Queue
	IvySynchronousBindingCallback *param = new IvySynchronousBindingCallback(target);
	param->type = FILTER_CB;
	param->app = app;
	param->id = id;
	param->regexp = _strdup(regexp);
	IvySynchroWnd::m_synchro->PostIvyCB( param );
}
void IvySynchronousBindingCallback::CallCallback()
{
	if ( target )
	{
		switch ( type )
		{
		case ADD_CB:
			target->OnAddBind( app, id, regexp );
			break;
		case REMOVE_CB:
			target->OnRemoveBind( app, id, regexp );
			break;
		case FILTER_CB:
			target->OnFilterBind( app, id, regexp );
			break;
		}
	}
	delete regexp;	
}

IvySynchronousDieCallback::IvySynchronousDieCallback( IvyDieCallback *cb )
{
	target = cb;
}
bool IvySynchronousDieCallback::OnDie(IvyApplication *app, int id, const char *arg )
{
	static int msg_count = 0;
	// duplicate on the Message Queue
	IvySynchronousDieCallback *param = new IvySynchronousDieCallback(target);
	param->app = app;
	param->id = id;
	
#ifdef UNDER_CE
		param->arg = _strdup( argv);
#else
		param->arg = _strdup( arg);
#endif

//	TRACE( "IvySynchronousMessageCallback::OnMessage msg count %d\n",wParam);
	IvySynchroWnd::m_synchro->PostIvyCB( param );
	return FALSE; // do not exit now wait for Quit on MessageQueue	
}
void IvySynchronousDieCallback::CallCallback()
{
	// duplicate on the Message Queue
	if ( !target ) return;
	if ( target->OnDie ( app, id, arg ) )
	{
		IvySynchroWnd::m_synchro->PostQuit();
	}
}