1 /*****************************************************************************
2 * connectioncontainer.cpp: ActiveX control for VLC
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
5 * Copyright (C) 2010 M2X BV
7 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
8 * Jean-Paul Saman <jpsaman@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
26 #include "connectioncontainer.h"
36 ////////////////////////////////////////////////////////////////////////////////////////////////
37 DEFINE_GUID(IID_IGlobalInterfaceTable, 0x00000146, 0x0000, 0x0000, 0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46);
38 DEFINE_GUID(CLSID_StdGlobalInterfaceTable, 0x00000323, 0x0000, 0x0000, 0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46);
40 const GUID IID_IGlobalInterfaceTable = { 0x00000146, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
41 const CLSID CLSID_StdGlobalInterfaceTable = { 0x00000323, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
42 ////////////////////////////////////////////////////////////////////////////////////////////////
44 /* this function object is used to return the value from a map pair */
45 struct VLCEnumConnectionsDereference
47 CONNECTDATA operator()(const map<DWORD,LPUNKNOWN>::iterator& i)
53 cd.dwCookie = i->first;
59 class VLCEnumConnections : public VLCEnumIterator<IID_IEnumConnections,
62 map<DWORD,LPUNKNOWN>::iterator,
63 VLCEnumConnectionsDereference>
66 VLCEnumConnections(map<DWORD,LPUNKNOWN> &m) :
67 VLCEnumIterator<IID_IEnumConnections,
70 map<DWORD,LPUNKNOWN>::iterator,
71 VLCEnumConnectionsDereference> (m.begin(), m.end())
75 ////////////////////////////////////////////////////////////////////////////////////////////////
77 /* this function object is used to retain the dereferenced iterator value */
78 struct VLCEnumConnectionPointsDereference
80 LPCONNECTIONPOINT operator()(const vector<LPCONNECTIONPOINT>::iterator& i)
82 LPCONNECTIONPOINT cp = *i;
88 class VLCEnumConnectionPoints: public VLCEnumIterator<IID_IEnumConnectionPoints,
89 IEnumConnectionPoints,
91 vector<LPCONNECTIONPOINT>::iterator,
92 VLCEnumConnectionPointsDereference>
95 VLCEnumConnectionPoints(vector<LPCONNECTIONPOINT>& v) :
96 VLCEnumIterator<IID_IEnumConnectionPoints,
97 IEnumConnectionPoints,
99 vector<LPCONNECTIONPOINT>::iterator,
100 VLCEnumConnectionPointsDereference> (v.begin(), v.end())
104 ////////////////////////////////////////////////////////////////////////////////////////////////
105 // Condition variable emulation
106 ////////////////////////////////////////////////////////////////////////////////////////////////
108 static void ConditionInit(HANDLE *handle)
110 *handle = CreateEvent(NULL, TRUE, FALSE, NULL);
111 assert(*handle != NULL);
114 static void ConditionDestroy(HANDLE *handle)
116 CloseHandle(*handle);
119 static void ConditionWait(HANDLE *handle, CRITICAL_SECTION *lock)
124 LeaveCriticalSection(lock);
125 dwWaitResult = WaitForSingleObjectEx(*handle, INFINITE, TRUE);
126 EnterCriticalSection(lock);
127 } while(dwWaitResult == WAIT_IO_COMPLETION);
129 assert(dwWaitResult != WAIT_ABANDONED); /* another thread failed to cleanup! */
130 assert(dwWaitResult != WAIT_FAILED);
134 static void ConditionSignal(HANDLE *handle)
139 ////////////////////////////////////////////////////////////////////////////////////////////////
140 // Event handling thread
142 // It bridges the gap between libvlc thread context and ActiveX/COM thread context.
143 ////////////////////////////////////////////////////////////////////////////////////////////////
144 DWORD WINAPI ThreadProcEventHandler(LPVOID lpParam)
147 VLCConnectionPointContainer *pCPC = (VLCConnectionPointContainer *)lpParam;
149 while(pCPC->isRunning)
151 EnterCriticalSection(&(pCPC->csEvents));
152 ConditionWait(&(pCPC->sEvents), &(pCPC->csEvents));
154 if (!pCPC->isRunning)
156 LeaveCriticalSection(&(pCPC->csEvents));
160 while(!pCPC->_q_events.empty())
162 VLCDispatchEvent *ev = pCPC->_q_events.front();
163 pCPC->_q_events.pop();
164 pCPC->_p_events->fireEvent(ev->_dispId, &ev->_dispParams);
167 LeaveCriticalSection(&(pCPC->csEvents));
174 ////////////////////////////////////////////////////////////////////////////////////////////////
175 // VLCConnectionPoint
176 ////////////////////////////////////////////////////////////////////////////////////////////////
178 VLCConnectionPoint::VLCConnectionPoint(IConnectionPointContainer *p_cpc, REFIID iid) :
179 _iid(iid), _p_cpc(p_cpc)
181 // Get the Global Interface Table per-process singleton:
182 CoCreateInstance(CLSID_StdGlobalInterfaceTable, 0,
183 CLSCTX_INPROC_SERVER,
184 IID_IGlobalInterfaceTable,
185 reinterpret_cast<void**>(&m_pGIT));
188 VLCConnectionPoint::~VLCConnectionPoint()
190 // Revoke interfaces from the GIT:
191 map<DWORD,LPUNKNOWN>::iterator end = _connections.end();
192 map<DWORD,LPUNKNOWN>::iterator iter = _connections.begin();
196 m_pGIT->RevokeInterfaceFromGlobal((DWORD)iter->second);
202 STDMETHODIMP VLCConnectionPoint::GetConnectionInterface(IID *iid)
211 STDMETHODIMP VLCConnectionPoint::GetConnectionPointContainer(LPCONNECTIONPOINTCONTAINER *ppCPC)
221 STDMETHODIMP VLCConnectionPoint::Advise(IUnknown *pUnk, DWORD *pdwCookie)
223 static DWORD dwCookieCounter = 0;
226 if( (NULL == pUnk) || (NULL == pdwCookie) )
229 hr = pUnk->QueryInterface(_iid, (LPVOID *)&pUnk);
233 hr = m_pGIT->RegisterInterfaceInGlobal( pUnk, _iid, &dwGITCookie );
236 *pdwCookie = ++dwCookieCounter;
237 _connections[*pdwCookie] = (LPUNKNOWN) dwGITCookie;
244 STDMETHODIMP VLCConnectionPoint::Unadvise(DWORD pdwCookie)
246 map<DWORD,LPUNKNOWN>::iterator pcd = _connections.find((DWORD)pdwCookie);
247 if( pcd != _connections.end() )
249 m_pGIT->RevokeInterfaceFromGlobal((DWORD)pcd->second);
250 _connections.erase(pdwCookie);
253 return CONNECT_E_NOCONNECTION;
256 STDMETHODIMP VLCConnectionPoint::EnumConnections(IEnumConnections **ppEnum)
261 *ppEnum = dynamic_cast<LPENUMCONNECTIONS>(new VLCEnumConnections(_connections));
263 return (NULL != *ppEnum ) ? S_OK : E_OUTOFMEMORY;
266 void VLCConnectionPoint::fireEvent(DISPID dispId, DISPPARAMS *pDispParams)
268 map<DWORD,LPUNKNOWN>::iterator end = _connections.end();
269 map<DWORD,LPUNKNOWN>::iterator iter = _connections.begin();
275 DWORD dwCookie = (DWORD)iter->second;
278 hr = m_pGIT->GetInterfaceFromGlobal( dwCookie, _iid,
279 reinterpret_cast<void **>(&pUnk) );
283 hr = pUnk->QueryInterface(_iid, (LPVOID *)&pDisp);
286 pDisp->Invoke(dispId, IID_NULL, LOCALE_USER_DEFAULT,
287 DISPATCH_METHOD, pDispParams, NULL, NULL, NULL);
296 void VLCConnectionPoint::firePropChangedEvent(DISPID dispId)
298 map<DWORD,LPUNKNOWN>::iterator end = _connections.end();
299 map<DWORD,LPUNKNOWN>::iterator iter = _connections.begin();
306 hr = m_pGIT->GetInterfaceFromGlobal( (DWORD)iter->second, IID_IUnknown,
307 reinterpret_cast<void **>(&pUnk) );
310 IPropertyNotifySink *pPropSink;
311 hr = pUnk->QueryInterface( IID_IPropertyNotifySink, (LPVOID *)&pPropSink );
314 pPropSink->OnChanged(dispId);
315 pPropSink->Release();
323 ////////////////////////////////////////////////////////////////////////////////////////////////
325 VLCDispatchEvent::~VLCDispatchEvent()
327 //clear event arguments
328 if( NULL != _dispParams.rgvarg )
330 for(unsigned int c = 0; c < _dispParams.cArgs; ++c)
331 VariantClear(_dispParams.rgvarg + c);
332 CoTaskMemFree(_dispParams.rgvarg);
334 if( NULL != _dispParams.rgdispidNamedArgs )
335 CoTaskMemFree(_dispParams.rgdispidNamedArgs);
338 ////////////////////////////////////////////////////////////////////////////////////////////////
339 // VLCConnectionPointContainer
340 ////////////////////////////////////////////////////////////////////////////////////////////////
342 VLCConnectionPointContainer::VLCConnectionPointContainer(VLCPlugin *p_instance) :
343 _p_instance(p_instance), freeze(FALSE), isRunning(TRUE)
345 _p_events = new VLCConnectionPoint(dynamic_cast<LPCONNECTIONPOINTCONTAINER>(this),
346 _p_instance->getDispEventID());
348 _v_cps.push_back(dynamic_cast<LPCONNECTIONPOINT>(_p_events));
350 _p_props = new VLCConnectionPoint(dynamic_cast<LPCONNECTIONPOINTCONTAINER>(this),
351 IID_IPropertyNotifySink);
353 _v_cps.push_back(dynamic_cast<LPCONNECTIONPOINT>(_p_props));
356 InitializeCriticalSection(&csEvents);
357 ConditionInit(&sEvents);
360 hThread = CreateThread(NULL, NULL, ThreadProcEventHandler,
361 dynamic_cast<LPVOID>(this), NULL, NULL);
364 VLCConnectionPointContainer::~VLCConnectionPointContainer()
366 EnterCriticalSection(&csEvents);
368 ConditionSignal(&sEvents);
369 LeaveCriticalSection(&csEvents);
372 /* nothing wait for thread to finish */;
373 } while(WaitForSingleObjectEx (hThread, INFINITE, TRUE) == WAIT_IO_COMPLETION);
374 CloseHandle(hThread);
376 ConditionDestroy(&sEvents);
377 DeleteCriticalSection(&csEvents);
383 STDMETHODIMP VLCConnectionPointContainer::EnumConnectionPoints(LPENUMCONNECTIONPOINTS *ppEnum)
388 *ppEnum = dynamic_cast<LPENUMCONNECTIONPOINTS>(new VLCEnumConnectionPoints(_v_cps));
390 return (NULL != *ppEnum ) ? S_OK : E_OUTOFMEMORY;
393 STDMETHODIMP VLCConnectionPointContainer::FindConnectionPoint(REFIID riid, IConnectionPoint **ppCP)
400 if( IID_IPropertyNotifySink == riid )
403 *ppCP = dynamic_cast<LPCONNECTIONPOINT>(_p_props);
405 else if( _p_instance->getDispEventID() == riid )
408 *ppCP = dynamic_cast<LPCONNECTIONPOINT>(_p_events);
411 return CONNECT_E_NOCONNECTION;
416 void VLCConnectionPointContainer::freezeEvents(BOOL bFreeze)
418 EnterCriticalSection(&csEvents);
420 LeaveCriticalSection(&csEvents);
423 void VLCConnectionPointContainer::fireEvent(DISPID dispId, DISPPARAMS* pDispParams)
425 EnterCriticalSection(&csEvents);
427 // queue event for later use when container is ready
428 _q_events.push(new VLCDispatchEvent(dispId, *pDispParams));
429 if( _q_events.size() > 1024 )
431 // too many events in queue, get rid of older one
432 delete _q_events.front();
435 ConditionSignal(&sEvents);
436 LeaveCriticalSection(&csEvents);
439 void VLCConnectionPointContainer::firePropChangedEvent(DISPID dispId)
442 _p_props->firePropChangedEvent(dispId);