1 /*****************************************************************************
2 * connectioncontainer.cpp: ActiveX control for VLC
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
6 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
24 #include "connectioncontainer.h"
30 ////////////////////////////////////////////////////////////////////////////////////////////////
32 /* this function object is used to return the value from a map pair */
33 struct VLCEnumConnectionsDereference
35 CONNECTDATA operator()(const map<DWORD,LPUNKNOWN>::iterator& i)
39 cd.dwCookie = i->first;
45 class VLCEnumConnections : public VLCEnumIterator<IID_IEnumConnections,
48 map<DWORD,LPUNKNOWN>::iterator,
49 VLCEnumConnectionsDereference>
52 VLCEnumConnections(map<DWORD,LPUNKNOWN> &m) :
53 VLCEnumIterator<IID_IEnumConnections,
56 map<DWORD,LPUNKNOWN>::iterator,
57 VLCEnumConnectionsDereference> (m.begin(), m.end())
61 ////////////////////////////////////////////////////////////////////////////////////////////////
63 /* this function object is used to retain the dereferenced iterator value */
64 struct VLCEnumConnectionPointsDereference
66 LPCONNECTIONPOINT operator()(const vector<LPCONNECTIONPOINT>::iterator& i)
68 LPCONNECTIONPOINT cp = *i;
74 class VLCEnumConnectionPoints: public VLCEnumIterator<IID_IEnumConnectionPoints,
75 IEnumConnectionPoints,
77 vector<LPCONNECTIONPOINT>::iterator,
78 VLCEnumConnectionPointsDereference>
81 VLCEnumConnectionPoints(vector<LPCONNECTIONPOINT>& v) :
82 VLCEnumIterator<IID_IEnumConnectionPoints,
83 IEnumConnectionPoints,
85 vector<LPCONNECTIONPOINT>::iterator,
86 VLCEnumConnectionPointsDereference> (v.begin(), v.end())
90 ////////////////////////////////////////////////////////////////////////////////////////////////
92 STDMETHODIMP VLCConnectionPoint::GetConnectionInterface(IID *iid)
101 STDMETHODIMP VLCConnectionPoint::GetConnectionPointContainer(LPCONNECTIONPOINTCONTAINER *ppCPC)
111 STDMETHODIMP VLCConnectionPoint::Advise(IUnknown *pUnk, DWORD *pdwCookie)
113 static DWORD dwCookieCounter = 0;
115 if( (NULL == pUnk) || (NULL == pdwCookie) )
120 *pdwCookie = ++dwCookieCounter;
121 _connections[*pdwCookie] = pUnk;
126 STDMETHODIMP VLCConnectionPoint::Unadvise(DWORD pdwCookie)
128 map<DWORD,LPUNKNOWN>::iterator pcd = _connections.find((DWORD)pdwCookie);
129 if( pcd != _connections.end() )
131 pcd->second->Release();
133 _connections.erase(pdwCookie);
136 return CONNECT_E_NOCONNECTION;
139 STDMETHODIMP VLCConnectionPoint::EnumConnections(IEnumConnections **ppEnum)
144 *ppEnum = dynamic_cast<LPENUMCONNECTIONS>(new VLCEnumConnections(_connections));
146 return (NULL != *ppEnum ) ? S_OK : E_OUTOFMEMORY;
149 void VLCConnectionPoint::fireEvent(DISPID dispId, DISPPARAMS *pDispParams)
151 map<DWORD,LPUNKNOWN>::iterator end = _connections.end();
152 map<DWORD,LPUNKNOWN>::iterator iter = _connections.begin();
156 LPUNKNOWN pUnk = iter->second;
160 if( SUCCEEDED(pUnk->QueryInterface(IID_IDispatch, (LPVOID *)&pDisp)) )
162 pDisp->Invoke(dispId, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, pDispParams, NULL, NULL, NULL);
170 void VLCConnectionPoint::firePropChangedEvent(DISPID dispId)
172 map<DWORD,LPUNKNOWN>::iterator end = _connections.end();
173 map<DWORD,LPUNKNOWN>::iterator iter = _connections.begin();
177 LPUNKNOWN pUnk = iter->second;
180 IPropertyNotifySink *pPropSink;
181 if( SUCCEEDED(pUnk->QueryInterface(IID_IPropertyNotifySink, (LPVOID *)&pPropSink)) )
183 pPropSink->OnChanged(dispId);
184 pPropSink->Release();
191 ////////////////////////////////////////////////////////////////////////////////////////////////
193 VLCDispatchEvent::~VLCDispatchEvent()
195 //clear event arguments
196 if( NULL != _dispParams.rgvarg )
198 for(unsigned int c=0; c<_dispParams.cArgs; ++c)
199 VariantClear(_dispParams.rgvarg+c);
200 CoTaskMemFree(_dispParams.rgvarg);
202 if( NULL != _dispParams.rgdispidNamedArgs )
203 CoTaskMemFree(_dispParams.rgdispidNamedArgs);
206 ////////////////////////////////////////////////////////////////////////////////////////////////
208 VLCConnectionPointContainer::VLCConnectionPointContainer(VLCPlugin *p_instance) :
209 _p_instance(p_instance), _b_freeze(FALSE)
211 _p_events = new VLCConnectionPoint(dynamic_cast<LPCONNECTIONPOINTCONTAINER>(this),
212 _p_instance->getDispEventID());
214 _v_cps.push_back(dynamic_cast<LPCONNECTIONPOINT>(_p_events));
216 _p_props = new VLCConnectionPoint(dynamic_cast<LPCONNECTIONPOINTCONTAINER>(this),
217 IID_IPropertyNotifySink);
219 _v_cps.push_back(dynamic_cast<LPCONNECTIONPOINT>(_p_props));
222 VLCConnectionPointContainer::~VLCConnectionPointContainer()
228 STDMETHODIMP VLCConnectionPointContainer::EnumConnectionPoints(LPENUMCONNECTIONPOINTS *ppEnum)
233 *ppEnum = dynamic_cast<LPENUMCONNECTIONPOINTS>(new VLCEnumConnectionPoints(_v_cps));
235 return (NULL != *ppEnum ) ? S_OK : E_OUTOFMEMORY;
238 STDMETHODIMP VLCConnectionPointContainer::FindConnectionPoint(REFIID riid, IConnectionPoint **ppCP)
245 if( IID_IPropertyNotifySink == riid )
248 *ppCP = dynamic_cast<LPCONNECTIONPOINT>(_p_props);
250 else if( _p_instance->getDispEventID() == riid )
253 *ppCP = dynamic_cast<LPCONNECTIONPOINT>(_p_events);
256 return CONNECT_E_NOCONNECTION;
261 void VLCConnectionPointContainer::freezeEvents(BOOL freeze)
265 // release queued events
266 while( ! _q_events.empty() )
268 VLCDispatchEvent *ev = _q_events.front();
270 _p_events->fireEvent(ev->_dispId, &ev->_dispParams);
277 void VLCConnectionPointContainer::fireEvent(DISPID dispId, DISPPARAMS* pDispParams)
281 // queue event for later use when container is ready
282 _q_events.push(new VLCDispatchEvent(dispId, *pDispParams));
283 if( _q_events.size() > 10 )
285 // too many events in queue, get rid of older one
286 delete _q_events.front();
292 _p_events->fireEvent(dispId, pDispParams);
296 void VLCConnectionPointContainer::firePropChangedEvent(DISPID dispId)
299 _p_props->firePropChangedEvent(dispId);