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)
41 cd.dwCookie = i->first;
47 class VLCEnumConnections : public VLCEnumIterator<IID_IEnumConnections,
50 map<DWORD,LPUNKNOWN>::iterator,
51 VLCEnumConnectionsDereference>
54 VLCEnumConnections(map<DWORD,LPUNKNOWN> &m) :
55 VLCEnumIterator<IID_IEnumConnections,
58 map<DWORD,LPUNKNOWN>::iterator,
59 VLCEnumConnectionsDereference> (m.begin(), m.end())
63 ////////////////////////////////////////////////////////////////////////////////////////////////
65 /* this function object is used to retain the dereferenced iterator value */
66 struct VLCEnumConnectionPointsDereference
68 LPCONNECTIONPOINT operator()(const vector<LPCONNECTIONPOINT>::iterator& i)
70 LPCONNECTIONPOINT cp = *i;
76 class VLCEnumConnectionPoints: public VLCEnumIterator<IID_IEnumConnectionPoints,
77 IEnumConnectionPoints,
79 vector<LPCONNECTIONPOINT>::iterator,
80 VLCEnumConnectionPointsDereference>
83 VLCEnumConnectionPoints(vector<LPCONNECTIONPOINT>& v) :
84 VLCEnumIterator<IID_IEnumConnectionPoints,
85 IEnumConnectionPoints,
87 vector<LPCONNECTIONPOINT>::iterator,
88 VLCEnumConnectionPointsDereference> (v.begin(), v.end())
92 ////////////////////////////////////////////////////////////////////////////////////////////////
94 STDMETHODIMP VLCConnectionPoint::GetConnectionInterface(IID *iid)
103 STDMETHODIMP VLCConnectionPoint::GetConnectionPointContainer(LPCONNECTIONPOINTCONTAINER *ppCPC)
113 STDMETHODIMP VLCConnectionPoint::Advise(IUnknown *pUnk, DWORD *pdwCookie)
115 static DWORD dwCookieCounter = 0;
117 if( (NULL == pUnk) || (NULL == pdwCookie) )
120 if( SUCCEEDED(pUnk->QueryInterface(_iid, (LPVOID *)&pUnk)) )
122 *pdwCookie = ++dwCookieCounter;
123 _connections[*pdwCookie] = pUnk;
126 return CONNECT_E_CANNOTCONNECT;
129 STDMETHODIMP VLCConnectionPoint::Unadvise(DWORD pdwCookie)
131 map<DWORD,LPUNKNOWN>::iterator pcd = _connections.find((DWORD)pdwCookie);
132 if( pcd != _connections.end() )
134 pcd->second->Release();
136 _connections.erase(pdwCookie);
139 return CONNECT_E_NOCONNECTION;
142 STDMETHODIMP VLCConnectionPoint::EnumConnections(IEnumConnections **ppEnum)
147 *ppEnum = dynamic_cast<LPENUMCONNECTIONS>(new VLCEnumConnections(_connections));
149 return (NULL != *ppEnum ) ? S_OK : E_OUTOFMEMORY;
152 void VLCConnectionPoint::fireEvent(DISPID dispId, DISPPARAMS *pDispParams)
154 map<DWORD,LPUNKNOWN>::iterator end = _connections.end();
155 map<DWORD,LPUNKNOWN>::iterator iter = _connections.begin();
159 LPUNKNOWN pUnk = iter->second;
163 if( SUCCEEDED(pUnk->QueryInterface(_iid, (LPVOID *)&pDisp)) )
165 pDisp->Invoke(dispId, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, pDispParams, NULL, NULL, NULL);
173 void VLCConnectionPoint::firePropChangedEvent(DISPID dispId)
175 map<DWORD,LPUNKNOWN>::iterator end = _connections.end();
176 map<DWORD,LPUNKNOWN>::iterator iter = _connections.begin();
180 LPUNKNOWN pUnk = iter->second;
183 IPropertyNotifySink *pPropSink;
184 if( SUCCEEDED(pUnk->QueryInterface(IID_IPropertyNotifySink, (LPVOID *)&pPropSink)) )
186 pPropSink->OnChanged(dispId);
187 pPropSink->Release();
194 ////////////////////////////////////////////////////////////////////////////////////////////////
196 VLCDispatchEvent::~VLCDispatchEvent()
198 //clear event arguments
199 if( NULL != _dispParams.rgvarg )
201 for(unsigned int c=0; c<_dispParams.cArgs; ++c)
202 VariantClear(_dispParams.rgvarg+c);
203 CoTaskMemFree(_dispParams.rgvarg);
205 if( NULL != _dispParams.rgdispidNamedArgs )
206 CoTaskMemFree(_dispParams.rgdispidNamedArgs);
209 ////////////////////////////////////////////////////////////////////////////////////////////////
211 VLCConnectionPointContainer::VLCConnectionPointContainer(VLCPlugin *p_instance) :
212 _p_instance(p_instance), _b_freeze(FALSE)
214 _p_events = new VLCConnectionPoint(dynamic_cast<LPCONNECTIONPOINTCONTAINER>(this),
215 _p_instance->getDispEventID());
217 _v_cps.push_back(dynamic_cast<LPCONNECTIONPOINT>(_p_events));
219 _p_props = new VLCConnectionPoint(dynamic_cast<LPCONNECTIONPOINTCONTAINER>(this),
220 IID_IPropertyNotifySink);
222 _v_cps.push_back(dynamic_cast<LPCONNECTIONPOINT>(_p_props));
225 VLCConnectionPointContainer::~VLCConnectionPointContainer()
231 STDMETHODIMP VLCConnectionPointContainer::EnumConnectionPoints(LPENUMCONNECTIONPOINTS *ppEnum)
236 *ppEnum = dynamic_cast<LPENUMCONNECTIONPOINTS>(new VLCEnumConnectionPoints(_v_cps));
238 return (NULL != *ppEnum ) ? S_OK : E_OUTOFMEMORY;
241 STDMETHODIMP VLCConnectionPointContainer::FindConnectionPoint(REFIID riid, IConnectionPoint **ppCP)
248 if( IID_IPropertyNotifySink == riid )
251 *ppCP = dynamic_cast<LPCONNECTIONPOINT>(_p_props);
253 else if( _p_instance->getDispEventID() == riid )
256 *ppCP = dynamic_cast<LPCONNECTIONPOINT>(_p_events);
259 return CONNECT_E_NOCONNECTION;
264 void VLCConnectionPointContainer::freezeEvents(BOOL freeze)
268 // release queued events
269 while( ! _q_events.empty() )
271 VLCDispatchEvent *ev = _q_events.front();
273 _p_events->fireEvent(ev->_dispId, &ev->_dispParams);
280 void VLCConnectionPointContainer::fireEvent(DISPID dispId, DISPPARAMS* pDispParams)
284 // queue event for later use when container is ready
285 _q_events.push(new VLCDispatchEvent(dispId, *pDispParams));
286 if( _q_events.size() > 10 )
288 // too many events in queue, get rid of older one
289 delete _q_events.front();
295 _p_events->fireEvent(dispId, pDispParams);
299 void VLCConnectionPointContainer::firePropChangedEvent(DISPID dispId)
302 _p_props->firePropChangedEvent(dispId);