connectioncontainer.cpp 12.7 KB
Newer Older
1 2 3
/*****************************************************************************
 * connectioncontainer.cpp: ActiveX control for VLC
 *****************************************************************************
4
 * Copyright (C) 2005 the VideoLAN team
5
 * Copyright (C) 2010 M2X BV
6 7
 *
 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
8
 *          Jean-Paul Saman <jpsaman@videolan.org>
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24
 *****************************************************************************/

25 26 27 28
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

29 30 31 32
#include "plugin.h"
#include "connectioncontainer.h"
#include "utils.h"

33 34
#include <atomic>

35 36
/* this function object is used to return the value from a map pair */
struct VLCEnumConnectionsDereference
37
{
38
    CONNECTDATA operator()(const std::map<DWORD,LPUNKNOWN>::iterator& i)
39
    {
40
        CONNECTDATA cd;
41

42 43
        i->second->AddRef();

44 45 46
        cd.dwCookie = i->first;
        cd.pUnk     = i->second;
        return cd;
47
    }
48 49
};

50 51 52 53 54
using VLCEnumConnections = VLCEnumIterator<IID_IEnumConnections,
                            IEnumConnections,
                            CONNECTDATA,
                            std::map<DWORD,LPUNKNOWN>,
                            VLCEnumConnectionsDereference>;
55 56

////////////////////////////////////////////////////////////////////////////////////////////////
57

58 59 60
/* this function object is used to retain the dereferenced iterator value */
struct VLCEnumConnectionPointsDereference
{
61
    LPCONNECTIONPOINT operator()(const std::vector<LPCONNECTIONPOINT>::iterator& i)
62 63 64 65 66 67 68
    {
        LPCONNECTIONPOINT cp = *i;
        cp->AddRef();
        return cp;
    }
};

69 70 71 72 73
using VLCEnumConnectionPoints = VLCEnumIterator<IID_IEnumConnectionPoints,
                                    IEnumConnectionPoints,
                                    LPCONNECTIONPOINT,
                                    std::vector<LPCONNECTIONPOINT>,
                                    VLCEnumConnectionPointsDereference>;
74
////////////////////////////////////////////////////////////////////////////////////////////////
75
// EventSystemProxyWnd
76
////////////////////////////////////////////////////////////////////////////////////////////////
77
class EventSystemProxyWnd
78
{
79 80 81 82
private:
    enum{
        WM_NEW_EVENT_NOTIFY = WM_USER+1
    };
83
    static LPCTSTR getClassName(void) { return TEXT("VLC ActiveX ESP Class"); }
84 85 86 87 88 89 90 91 92 93 94 95 96
    static void RegisterWndClassName();
    static void UnRegisterWndClassName();
    static LRESULT CALLBACK ESPWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

    static HINSTANCE _hInstance;
    static ATOM _ESP_wndclass_atom;
    HWND _hWnd;

public:
    static EventSystemProxyWnd* CreateESPWindow(HINSTANCE hInstance, VLCConnectionPointContainer *pCPC);

private:
    EventSystemProxyWnd(HWND hWnd, VLCConnectionPointContainer *pCPC)
97
        : _hWnd(hWnd), _pCPC(pCPC)
98
    {
99 100 101
        // Initialize this out of members initialization list because MSVC
        // doesn't implement atomic_int constructor until VS 2015
        _HasUnprocessedNotify = false;
102
    }
103 104 105 106
    void ProcessNotify();

public:
    void DestroyWindow()
107 108
        {::DestroyWindow(_hWnd);}

109 110 111 112 113 114

    //allowed to invoke from any thread
    void NewEventNotify();

private:
    VLCConnectionPointContainer *_pCPC;
115
    std::atomic_bool _HasUnprocessedNotify;
116 117 118 119
};

HINSTANCE EventSystemProxyWnd::_hInstance=0;
ATOM EventSystemProxyWnd::_ESP_wndclass_atom=0;
120 121


122
void EventSystemProxyWnd::RegisterWndClassName()
123
{
124 125
    WNDCLASS wClass;
    memset(&wClass, 0, sizeof(WNDCLASS));
126

127 128 129 130 131
    if( ! GetClassInfo(_hInstance, getClassName(), &wClass) )
    {
        wClass.lpfnWndProc    = ESPWindowProc;
        wClass.hInstance      = _hInstance;
        wClass.lpszClassName  = getClassName();
132

133 134 135 136 137 138
        _ESP_wndclass_atom = RegisterClass(&wClass);
    }
    else
    {
        _ESP_wndclass_atom = 0;
    }
139 140
}

141
void EventSystemProxyWnd::UnRegisterWndClassName()
142
{
143 144 145 146
    if(0 != _ESP_wndclass_atom){
        UnregisterClass(MAKEINTATOM(_ESP_wndclass_atom), _hInstance);
        _ESP_wndclass_atom = 0;
    }
147 148
}

149
LRESULT CALLBACK EventSystemProxyWnd::ESPWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
150
{
151
    EventSystemProxyWnd* ESP = reinterpret_cast<EventSystemProxyWnd*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
152

153
    switch( uMsg )
154
    {
155 156 157
        case WM_CREATE:{
            CREATESTRUCT* CreateStruct = (CREATESTRUCT*)(lParam);
            VLCConnectionPointContainer * pCPC = reinterpret_cast<VLCConnectionPointContainer *>(CreateStruct->lpCreateParams);
158

159 160
            ESP = new EventSystemProxyWnd(hWnd, pCPC);
            SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(ESP));
161 162
            break;
        }
163 164 165 166 167 168 169 170 171 172 173 174
        case WM_NCDESTROY:
            delete ESP;
            SetWindowLongPtr(hWnd, GWLP_USERDATA, 0);
            break;
        case WM_NEW_EVENT_NOTIFY:
            ESP->ProcessNotify();
            break;
        default:
            return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    return 0;
}
175

176 177 178 179
EventSystemProxyWnd* EventSystemProxyWnd::CreateESPWindow(HINSTANCE hInstance, VLCConnectionPointContainer *pCPC)
{
    //save hInstance for future use
    _hInstance = hInstance;
180

181
    RegisterWndClassName();
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
    HWND hWnd = CreateWindow(getClassName(),
                             0,
                             0,
                             0, 0, 0, 0,
                             0,
                             0,
                             _hInstance,
                             pCPC
                             );

    if(hWnd)
        return reinterpret_cast<EventSystemProxyWnd*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));

    return 0;
}

void EventSystemProxyWnd::NewEventNotify()
{
    if(!_HasUnprocessedNotify){
        _HasUnprocessedNotify=true;
        PostMessage(_hWnd, WM_NEW_EVENT_NOTIFY, 0, 0);
    }
}

void EventSystemProxyWnd::ProcessNotify()
{
    while(_pCPC->isRunning){
        VLCDispatchEvent *ev = 0;
        EnterCriticalSection(&(_pCPC->csEvents));
        if(!_pCPC->_q_events.empty()){
            ev = _pCPC->_q_events.front();
            _pCPC->_q_events.pop();
215
        }
216 217 218 219 220 221 222
        LeaveCriticalSection(&(_pCPC->csEvents));

        if(ev){
            _pCPC->_p_events->fireEvent(ev->_dispId, &ev->_dispParams);
            delete ev;
        }
        else break;
223
    }
224
    _HasUnprocessedNotify=false;
225 226 227 228
}

////////////////////////////////////////////////////////////////////////////////////////////////
// VLCConnectionPoint
229 230
////////////////////////////////////////////////////////////////////////////////////////////////

231 232 233
VLCConnectionPoint::VLCConnectionPoint(IConnectionPointContainer *p_cpc, REFIID iid) :
        _iid(iid), _p_cpc(p_cpc)
{
234
}
235 236 237 238

VLCConnectionPoint::~VLCConnectionPoint()
{
    // Revoke interfaces from the GIT:
239
    for (auto& p : _connections)
240
    {
241
        p.second->Release();
242
    }
243
    _connections.clear();
244
}
245

246 247
STDMETHODIMP VLCConnectionPoint::GetConnectionInterface(IID *iid)
{
248
    if( iid == nullptr )
249 250 251 252
        return E_POINTER;

    *iid = _iid;
    return S_OK;
253
}
254 255 256

STDMETHODIMP VLCConnectionPoint::GetConnectionPointContainer(LPCONNECTIONPOINTCONTAINER *ppCPC)
{
257
    if( ppCPC == nullptr )
258 259 260 261 262
        return E_POINTER;

    _p_cpc->AddRef();
    *ppCPC = _p_cpc;
    return S_OK;
263
}
264 265 266

STDMETHODIMP VLCConnectionPoint::Advise(IUnknown *pUnk, DWORD *pdwCookie)
{
267
    static DWORD dwCookieCounter = 0;
268
    HRESULT hr;
269

270
    if( (pUnk == nullptr) || (pdwCookie == nullptr) )
271 272
        return E_POINTER;

273 274
    hr = pUnk->QueryInterface(_iid, (LPVOID *)&pUnk);
    if( SUCCEEDED(hr) )
275
    {
276
        *pdwCookie = ++dwCookieCounter;
277 278 279
        char buff[256];
        sprintf(buff, "+++++++++++++++++ Adding listener in %p : %p\n", this, pUnk);
        OutputDebugStringA(buff);
280
        _connections[*pdwCookie] = pUnk;
281
    }
282
    return hr;
283
}
284 285 286

STDMETHODIMP VLCConnectionPoint::Unadvise(DWORD pdwCookie)
{
287
    auto pcd = _connections.find((DWORD)pdwCookie);
288
    if( pcd != _connections.end() )
289
    {
290 291
        pcd->second->Release();
        _connections.erase(pcd);
292
        return S_OK;
293 294
    }
    return CONNECT_E_NOCONNECTION;
295
}
296 297 298 299 300 301

STDMETHODIMP VLCConnectionPoint::EnumConnections(IEnumConnections **ppEnum)
{
    if( NULL == ppEnum )
        return E_POINTER;

302
    *ppEnum = new VLCEnumConnections(_connections);
303 304 305
    (*ppEnum)->AddRef();
    return S_OK;
}
306

307
void VLCConnectionPoint::fireEvent(DISPID dispId, DISPPARAMS *pDispParams)
308
{
309
    for (auto& p : _connections)
310
    {
311
        LPUNKNOWN pUnk = p.second;
312

313
        IDispatch *pDisp;
314
        HRESULT hr = pUnk->QueryInterface(_iid, (LPVOID *)&pDisp);
315
        if( SUCCEEDED(hr) )
316
        {
317 318 319
            pDisp->Invoke(dispId, IID_NULL, LOCALE_USER_DEFAULT,
                          DISPATCH_METHOD, pDispParams, NULL, NULL, NULL);
            pDisp->Release();
320 321
        }
    }
322
}
323 324 325

void VLCConnectionPoint::firePropChangedEvent(DISPID dispId)
{
326
    for (auto& p : _connections)
327
    {
328
        LPUNKNOWN pUnk = p.second;
329 330
        HRESULT hr;

331 332
        IPropertyNotifySink *pPropSink;
        hr = pUnk->QueryInterface( IID_IPropertyNotifySink, (LPVOID *)&pPropSink );
333
        if( SUCCEEDED(hr) )
334
        {
335 336
            pPropSink->OnChanged(dispId);
            pPropSink->Release();
337 338
        }
    }
339
}
340 341 342

////////////////////////////////////////////////////////////////////////////////////////////////

343 344 345
VLCDispatchEvent::~VLCDispatchEvent()
{
    //clear event arguments
346
    if( _dispParams.rgvarg != nullptr )
347
    {
348 349
        for(unsigned int c = 0; c < _dispParams.cArgs; ++c)
            VariantClear(_dispParams.rgvarg + c);
350 351
        CoTaskMemFree(_dispParams.rgvarg);
    }
352
    if( _dispParams.rgdispidNamedArgs != nullptr )
353
        CoTaskMemFree(_dispParams.rgdispidNamedArgs);
354
}
355

356 357
////////////////////////////////////////////////////////////////////////////////////////////////
// VLCConnectionPointContainer
358
////////////////////////////////////////////////////////////////////////////////////////////////
359
extern HMODULE DllGetModule();
360
VLCConnectionPointContainer::VLCConnectionPointContainer(VLCPlugin *p_instance) :
361
    _ESProxyWnd(0), _p_instance(p_instance), isRunning(TRUE), freeze(FALSE)
362
{
363
    _p_events = new VLCConnectionPoint(this, _p_instance->getDispEventID());
364

365
    _v_cps.push_back(_p_events);
366

367
    _p_props = new VLCConnectionPoint(this, IID_IPropertyNotifySink);
368

369
    _v_cps.push_back(_p_props);
370 371 372 373

    // init protection
    InitializeCriticalSection(&csEvents);

374
    _ESProxyWnd = EventSystemProxyWnd::CreateESPWindow(DllGetModule(), this);
375
}
376 377 378

VLCConnectionPointContainer::~VLCConnectionPointContainer()
{
379
    isRunning = FALSE;
380
    freeze = TRUE;
381

382 383 384
    if(_ESProxyWnd)
        _ESProxyWnd->DestroyWindow();
    _ESProxyWnd=0;
385 386 387

    DeleteCriticalSection(&csEvents);

388 389
    _v_cps.clear();
    while(!_q_events.empty()) {
390
        delete _q_events.front();
391
        _q_events.pop();
392
    }
393

394 395
    delete _p_props;
    delete _p_events;
396
}
397 398 399 400 401 402

STDMETHODIMP VLCConnectionPointContainer::EnumConnectionPoints(LPENUMCONNECTIONPOINTS *ppEnum)
{
    if( NULL == ppEnum )
        return E_POINTER;

403
    *ppEnum = new VLCEnumConnectionPoints(_v_cps);
404
    (*ppEnum)->AddRef();
405

406 407
    return S_OK;
}
408 409 410 411 412 413 414 415 416

STDMETHODIMP VLCConnectionPointContainer::FindConnectionPoint(REFIID riid, IConnectionPoint **ppCP)
{
    if( NULL == ppCP )
        return E_POINTER;

    *ppCP = NULL;

    if( IID_IPropertyNotifySink == riid )
417
        *ppCP = _p_props;
418
    else if( _p_instance->getDispEventID() == riid )
419
        *ppCP = _p_events;
420 421
    else
        return CONNECT_E_NOCONNECTION;
422
    (*ppCP)->AddRef();
423
    return NOERROR;
424
}
425

426
void VLCConnectionPointContainer::freezeEvents(BOOL bFreeze)
427
{
428 429 430
    EnterCriticalSection(&csEvents);
    freeze = bFreeze;
    LeaveCriticalSection(&csEvents);
431
}
432

433 434
void VLCConnectionPointContainer::fireEvent(DISPID dispId, DISPPARAMS* pDispParams)
{
435 436
    if(_ESProxyWnd){
        EnterCriticalSection(&csEvents);
437

438 439 440 441 442 443 444 445 446 447
        // queue event for later use when container is ready
        _q_events.push(new VLCDispatchEvent(dispId, *pDispParams));
        if( _q_events.size() > 1024 )
        {
            // too many events in queue, get rid of older one
            delete _q_events.front();
            _q_events.pop();
        }
        LeaveCriticalSection(&csEvents);
        _ESProxyWnd->NewEventNotify();
448
    }
449
}
450 451 452

void VLCConnectionPointContainer::firePropChangedEvent(DISPID dispId)
{
453
    if( ! freeze )
454
        _p_props->firePropChangedEvent(dispId);
455
}
456