plugin.cpp 41.7 KB
Newer Older
1 2 3
/*****************************************************************************
 * plugin.cpp: ActiveX control for VLC
 *****************************************************************************
4
 * Copyright (C) 2006-2010 the VideoLAN team
5 6
 *
 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
7
 *          Jean-Paul Saman <jpsaman@videolan.org>
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 *****************************************************************************/

#include "plugin.h"

#include "oleobject.h"
#include "olecontrol.h"
#include "oleinplaceobject.h"
#include "oleinplaceactiveobject.h"
#include "persistpropbag.h"
#include "persiststreaminit.h"
#include "persiststorage.h"
#include "provideclassinfo.h"
#include "connectioncontainer.h"
#include "objectsafety.h"
#include "vlccontrol.h"
37
#include "vlccontrol2.h"
38
#include "viewobject.h"
39
#include "dataobject.h"
40
#include "supporterrorinfo.h"
41 42 43

#include "utils.h"

44
#include <stdio.h>
45 46
#include <string.h>
#include <winreg.h>
47 48 49 50
#include <winuser.h>
#include <servprov.h>
#include <shlwapi.h>
#include <wininet.h>
51
#include <assert.h>
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

using namespace std;

////////////////////////////////////////////////////////////////////////
//class factory

static LRESULT CALLBACK VLCInPlaceClassWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    VLCPlugin *p_instance = reinterpret_cast<VLCPlugin *>(GetWindowLongPtr(hWnd, GWLP_USERDATA));

    switch( uMsg )
    {
        case WM_ERASEBKGND:
            return 1L;

        case WM_PAINT:
            PAINTSTRUCT ps;
            RECT pr;
            if( GetUpdateRect(hWnd, &pr, FALSE) )
            {
71 72
                RECT bounds;
                GetClientRect(hWnd, &bounds);
73
                BeginPaint(hWnd, &ps);
74
                p_instance->onPaint(ps.hdc, bounds, pr);
75 76 77 78 79 80 81 82 83
                EndPaint(hWnd, &ps);
            }
            return 0L;

        default:
            return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
};

84
VLCPluginClass::VLCPluginClass(LONG *p_class_ref, HINSTANCE hInstance, REFCLSID rclsid) :
85
    _p_class_ref(p_class_ref),
86
    _hinstance(hInstance),
87
    _classid(rclsid),
88
    _inplace_picture(NULL)
89 90 91 92 93
{
    WNDCLASS wClass;

    if( ! GetClassInfo(hInstance, getInPlaceWndClassName(), &wClass) )
    {
94
        wClass.style          = CS_NOCLOSE|CS_DBLCLKS;
95 96 97 98 99 100 101 102 103
        wClass.lpfnWndProc    = VLCInPlaceClassWndProc;
        wClass.cbClsExtra     = 0;
        wClass.cbWndExtra     = 0;
        wClass.hInstance      = hInstance;
        wClass.hIcon          = NULL;
        wClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wClass.hbrBackground  = NULL;
        wClass.lpszMenuName   = NULL;
        wClass.lpszClassName  = getInPlaceWndClassName();
104

105 106 107 108 109 110 111
        _inplace_wndclass_atom = RegisterClass(&wClass);
    }
    else
    {
        _inplace_wndclass_atom = 0;
    }

112
    HBITMAP hbitmap = (HBITMAP)LoadImage(getHInstance(), MAKEINTRESOURCE(2), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
113 114 115 116 117 118 119 120
    if( NULL != hbitmap )
    {
        PICTDESC pictDesc;

        pictDesc.cbSizeofstruct = sizeof(PICTDESC);
        pictDesc.picType        = PICTYPE_BITMAP;
        pictDesc.bmp.hbitmap    = hbitmap;
        pictDesc.bmp.hpal       = NULL;
121

122 123 124
        if( FAILED(OleCreatePictureIndirect(&pictDesc, IID_IPicture, TRUE, reinterpret_cast<LPVOID*>(&_inplace_picture))) )
            _inplace_picture = NULL;
    }
125 126 127 128 129 130 131 132
    AddRef();
};

VLCPluginClass::~VLCPluginClass()
{
    if( 0 != _inplace_wndclass_atom )
        UnregisterClass(MAKEINTATOM(_inplace_wndclass_atom), _hinstance);

133 134
    if( NULL != _inplace_picture )
        _inplace_picture->Release();
135 136 137 138 139 140 141
};

STDMETHODIMP VLCPluginClass::QueryInterface(REFIID riid, void **ppv)
{
    if( NULL == ppv )
        return E_INVALIDARG;

142 143
    if( (IID_IUnknown == riid)
     || (IID_IClassFactory == riid) )
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
    {
        AddRef();
        *ppv = reinterpret_cast<LPVOID>(this);

        return NOERROR;
    }

    *ppv = NULL;

    return E_NOINTERFACE;
};

STDMETHODIMP_(ULONG) VLCPluginClass::AddRef(void)
{
    return InterlockedIncrement(_p_class_ref);
};

STDMETHODIMP_(ULONG) VLCPluginClass::Release(void)
{
    ULONG refcount = InterlockedDecrement(_p_class_ref);
    if( 0 == refcount )
    {
        delete this;
        return 0;
    }
    return refcount;
};

172
STDMETHODIMP VLCPluginClass::CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, void **ppv)
173 174 175 176 177 178
{
    if( NULL == ppv )
        return E_POINTER;

    *ppv = NULL;

179
    if( (NULL != pUnkOuter) && (IID_IUnknown != riid) ) {
180
        return CLASS_E_NOAGGREGATION;
181
    }
182

183
    VLCPlugin *plugin = new VLCPlugin(this, pUnkOuter);
184 185 186
    if( NULL != plugin )
    {
        HRESULT hr = plugin->QueryInterface(riid, ppv);
187
        // the following will destroy the object if QueryInterface() failed
188 189 190 191 192 193 194 195 196 197
        plugin->Release();
        return hr;
    }
    return E_OUTOFMEMORY;
};

STDMETHODIMP VLCPluginClass::LockServer(BOOL fLock)
{
    if( fLock )
        AddRef();
198
    else
199 200 201 202 203 204 205
        Release();

    return S_OK;
};

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

206
VLCPlugin::VLCPlugin(VLCPluginClass *p_class, LPUNKNOWN pUnkOuter) :
207 208 209
    _inplacewnd(NULL),
    _p_class(p_class),
    _i_ref(1UL),
210
    _p_libvlc(NULL),
JP Dinger's avatar
JP Dinger committed
211 212 213
    _p_mlist(NULL),
    _p_mplayer(NULL),
    _i_midx(-1),
214
    _i_codepage(CP_ACP),
215
    _b_usermode(TRUE)
216
{
217 218 219 220 221
    /*
    ** bump refcount to avoid recursive release from
    ** following interfaces when releasing this interface
    */
    AddRef();
222 223 224 225 226 227 228 229 230 231 232 233
    p_class->AddRef();

    vlcOleControl = new VLCOleControl(this);
    vlcOleInPlaceObject = new VLCOleInPlaceObject(this);
    vlcOleInPlaceActiveObject = new VLCOleInPlaceActiveObject(this);
    vlcPersistStorage = new VLCPersistStorage(this);
    vlcPersistStreamInit = new VLCPersistStreamInit(this);
    vlcPersistPropertyBag = new VLCPersistPropertyBag(this);
    vlcProvideClassInfo = new VLCProvideClassInfo(this);
    vlcConnectionPointContainer = new VLCConnectionPointContainer(this);
    vlcObjectSafety = new VLCObjectSafety(this);
    vlcControl = new VLCControl(this);
234
    vlcControl2 = new VLCControl2(this);
235
    vlcViewObject = new VLCViewObject(this);
236
    vlcDataObject = new VLCDataObject(this);
237
    vlcOleObject = new VLCOleObject(this);
238
    vlcSupportErrorInfo = new VLCSupportErrorInfo(this);
239 240 241 242 243

    // configure controlling IUnknown interface for implemented interfaces
    this->pUnkOuter = (NULL != pUnkOuter) ? pUnkOuter : dynamic_cast<LPUNKNOWN>(this);

    // default picure
244
    _p_pict = p_class->getInPlacePict();
Damien Fouilleul's avatar
Damien Fouilleul committed
245

246 247
    // make sure that persistable properties are initialized
    onInit();
248 249 250 251
};

VLCPlugin::~VLCPlugin()
{
252
    delete vlcSupportErrorInfo;
253
    delete vlcOleObject;
254
    delete vlcDataObject;
255
    delete vlcViewObject;
256
    delete vlcControl2;
257 258 259 260 261 262 263 264
    delete vlcControl;
    delete vlcConnectionPointContainer;
    delete vlcProvideClassInfo;
    delete vlcPersistPropertyBag;
    delete vlcPersistStreamInit;
    delete vlcPersistStorage;
    delete vlcOleInPlaceActiveObject;
    delete vlcOleInPlaceObject;
265
    delete vlcObjectSafety;
266

267 268 269
    delete vlcOleControl;
    if( _p_pict )
        _p_pict->Release();
270

271
    SysFreeString(_bstr_mrl);
272
    SysFreeString(_bstr_baseurl);
273

274 275
    if( _p_mplayer )
    {
276 277 278
        if( isPlaying() )
            playlist_stop();

279 280 281 282
        player_unregister_events();
        libvlc_media_player_release(_p_mplayer);
        _p_mplayer=NULL;
    }
JP Dinger's avatar
JP Dinger committed
283 284 285
    if( _p_mlist )   { libvlc_media_list_release(_p_mlist); _p_mlist=NULL; }
    if( _p_libvlc )  { libvlc_release(_p_libvlc); _p_libvlc=NULL; }

286
    _p_class->Release();
287
    Release();
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
};

STDMETHODIMP VLCPlugin::QueryInterface(REFIID riid, void **ppv)
{
    if( NULL == ppv )
        return E_INVALIDARG;

    if( IID_IUnknown == riid )
        *ppv = reinterpret_cast<LPVOID>(this);
    else if( IID_IOleObject == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcOleObject);
    else if( IID_IOleControl == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcOleControl);
    else if( IID_IOleWindow == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
    else if( IID_IOleInPlaceObject == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
    else if( IID_IOleInPlaceActiveObject == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceActiveObject);
    else if( IID_IPersist == riid )
308
        *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    else if( IID_IPersistStreamInit == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
    else if( IID_IPersistStorage == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcPersistStorage);
    else if( IID_IPersistPropertyBag == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcPersistPropertyBag);
    else if( IID_IProvideClassInfo == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
    else if( IID_IProvideClassInfo2 == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
    else if( IID_IConnectionPointContainer == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcConnectionPointContainer);
    else if( IID_IObjectSafety == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcObjectSafety);
    else if( IID_IDispatch == riid )
324 325 326
        *ppv = (CLSID_VLCPlugin2 == getClassID()) ?
                reinterpret_cast<LPVOID>(vlcControl2) :
                reinterpret_cast<LPVOID>(vlcControl);
327 328
    else if( IID_IVLCControl == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcControl);
329 330
    else if( IID_IVLCControl2 == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcControl2);
331 332
    else if( IID_IViewObject == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
Damien Fouilleul's avatar
Damien Fouilleul committed
333 334
    else if( IID_IViewObject2 == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
335 336
    else if( IID_IDataObject == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcDataObject);
337 338
    else if( IID_ISupportErrorInfo == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcSupportErrorInfo);
339 340 341 342
    else
    {
        *ppv = NULL;
        return E_NOINTERFACE;
Damien Fouilleul's avatar
Damien Fouilleul committed
343
    }
344 345
    ((LPUNKNOWN)*ppv)->AddRef();
    return NOERROR;
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
};

STDMETHODIMP_(ULONG) VLCPlugin::AddRef(void)
{
    return InterlockedIncrement((LONG *)&_i_ref);
};

STDMETHODIMP_(ULONG) VLCPlugin::Release(void)
{
    if( ! InterlockedDecrement((LONG *)&_i_ref) )
    {
        delete this;
        return 0;
    }
    return _i_ref;
};

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

365
HRESULT VLCPlugin::onInit(void)
366
{
367
    if( NULL == _p_libvlc )
368
    {
369
        // initialize persistable properties
370 371 372
        _b_autoplay   = TRUE;
        _b_autoloop   = FALSE;
        _b_toolbar    = FALSE;
373
        _bstr_baseurl = NULL;
374 375 376 377 378 379
        _bstr_mrl     = NULL;
        _b_visible    = TRUE;
        _b_mute       = FALSE;
        _i_volume     = 50;
        _i_time       = 0;
        _i_backcolor  = 0;
380 381 382 383 384 385
        // set default/preferred size (320x240) pixels in HIMETRIC
        HDC hDC = CreateDevDC(NULL);
        _extent.cx = 320;
        _extent.cy = 240;
        HimetricFromDP(hDC, (LPPOINT)&_extent, 1);
        DeleteDC(hDC);
386 387 388

        return S_OK;
    }
389
    return CO_E_ALREADYINITIALIZED;
390
};
391 392 393

HRESULT VLCPlugin::onLoad(void)
{
394
    if( SysStringLen(_bstr_baseurl) == 0 )
395
    {
396
        /*
397
        ** try to retreive the base URL using the client site moniker, which for Internet Explorer
398
        ** is the URL of the page the plugin is embedded into.
399
        */
400
        LPOLECLIENTSITE pClientSite;
401
        if( SUCCEEDED(vlcOleObject->GetClientSite(&pClientSite)) && (NULL != pClientSite) )
402 403 404 405 406 407 408 409
        {
            IBindCtx *pBC = 0;
            if( SUCCEEDED(CreateBindCtx(0, &pBC)) )
            {
                LPMONIKER pContMoniker = NULL;
                if( SUCCEEDED(pClientSite->GetMoniker(OLEGETMONIKER_ONLYIFTHERE,
                                OLEWHICHMK_CONTAINER, &pContMoniker)) )
                {
410 411
                    LPOLESTR base_url;
                    if( SUCCEEDED(pContMoniker->GetDisplayName(pBC, NULL, &base_url)) )
412
                    {
413 414 415
                        /*
                        ** check that the moniker name is a URL
                        */
416
                        if( UrlIsW(base_url, URLIS_URL) )
417
                        {
418
                            /* copy base URL */
419
                            _bstr_baseurl = SysAllocString(base_url);
420
                        }
421
                        CoTaskMemFree(base_url);
422 423 424 425
                    }
                }
            }
        }
426 427 428 429 430
    }
    setDirty(FALSE);
    return S_OK;
};

JP Dinger's avatar
JP Dinger committed
431
void VLCPlugin::initVLC()
432
{
433 434
    extern HMODULE DllGetModule();

JP Dinger's avatar
JP Dinger committed
435 436 437 438 439
    /*
    ** default initialization options
    */
    const char *ppsz_argv[32] = { };
    int   ppsz_argc = 0;
440

JP Dinger's avatar
JP Dinger committed
441 442 443 444
    char p_progpath[MAX_PATH];
    {
        TCHAR w_progpath[MAX_PATH];
        DWORD len = GetModuleFileName(DllGetModule(), w_progpath, MAX_PATH);
445
        w_progpath[MAX_PATH-1] = '\0';
JP Dinger's avatar
JP Dinger committed
446
        if( len > 0 )
447
        {
JP Dinger's avatar
JP Dinger committed
448 449
            len = WideCharToMultiByte(CP_UTF8, 0, w_progpath, len, p_progpath,
                       sizeof(p_progpath)-1, NULL, NULL);
450 451
            if( len > 0 )
            {
JP Dinger's avatar
JP Dinger committed
452 453
                p_progpath[len] = '\0';
                ppsz_argv[0] = p_progpath;
454 455
            }
        }
JP Dinger's avatar
JP Dinger committed
456
    }
457

JP Dinger's avatar
JP Dinger committed
458
    ppsz_argv[ppsz_argc++] = "-vv";
459

JP Dinger's avatar
JP Dinger committed
460 461 462 463 464 465 466 467 468
    HKEY h_key;
    char p_pluginpath[MAX_PATH];
    if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, TEXT("Software\\VideoLAN\\VLC"),
                      0, KEY_READ, &h_key ) == ERROR_SUCCESS )
    {
        DWORD i_type, i_data = MAX_PATH;
        TCHAR w_pluginpath[MAX_PATH];
        if( RegQueryValueEx( h_key, TEXT("InstallDir"), 0, &i_type,
                             (LPBYTE)w_pluginpath, &i_data ) == ERROR_SUCCESS )
469
        {
470
            w_pluginpath[MAX_PATH-1] = '\0';
JP Dinger's avatar
JP Dinger committed
471
            if( i_type == REG_SZ )
472
            {
JP Dinger's avatar
JP Dinger committed
473 474
                if( WideCharToMultiByte(CP_UTF8, 0, w_pluginpath, -1, p_pluginpath,
                         sizeof(p_pluginpath)-sizeof("\\plugins")+1, NULL, NULL) )
475
                {
JP Dinger's avatar
JP Dinger committed
476 477 478
                    strcat( p_pluginpath, "\\plugins" );
                    ppsz_argv[ppsz_argc++] = "--plugin-path";
                    ppsz_argv[ppsz_argc++] = p_pluginpath;
479 480
                }
            }
481
        }
JP Dinger's avatar
JP Dinger committed
482 483
        RegCloseKey( h_key );
    }
484

JP Dinger's avatar
JP Dinger committed
485 486
    // make sure plugin isn't affected with VLC single instance mode
    ppsz_argv[ppsz_argc++] = "--no-one-instance";
487

JP Dinger's avatar
JP Dinger committed
488
    /* common settings */
489
    ppsz_argv[ppsz_argc++] = "-vv";
JP Dinger's avatar
JP Dinger committed
490 491 492
    ppsz_argv[ppsz_argc++] = "--no-stats";
    ppsz_argv[ppsz_argc++] = "--no-media-library";
    ppsz_argv[ppsz_argc++] = "--intf=dummy";
493 494
    ppsz_argv[ppsz_argc++] = "--no-video-title-show";

495

JP Dinger's avatar
JP Dinger committed
496 497 498
    // loop mode is a configuration option only
    if( _b_autoloop )
        ppsz_argv[ppsz_argc++] = "--loop";
499

500 501
    _p_libvlc = libvlc_new(ppsz_argc, ppsz_argv);
    if( !_p_libvlc )
JP Dinger's avatar
JP Dinger committed
502
        return;
503

504
    _p_mlist = libvlc_media_list_new(_p_libvlc);
505

JP Dinger's avatar
JP Dinger committed
506 507 508 509 510 511 512 513 514 515 516 517
    // initial playlist item
    if( SysStringLen(_bstr_mrl) > 0 )
    {
        char *psz_mrl = NULL;

        if( SysStringLen(_bstr_baseurl) > 0 )
        {
            /*
            ** if the MRL a relative URL, we should end up with an absolute URL
            */
            LPWSTR abs_url = CombineURL(_bstr_baseurl, _bstr_mrl);
            if( NULL != abs_url )
518
            {
JP Dinger's avatar
JP Dinger committed
519 520
                psz_mrl = CStrFromWSTR(CP_UTF8, abs_url, wcslen(abs_url));
                CoTaskMemFree(abs_url);
521 522 523 524
            }
            else
            {
                psz_mrl = CStrFromBSTR(CP_UTF8, _bstr_mrl);
525
            }
JP Dinger's avatar
JP Dinger committed
526 527 528 529 530 531 532 533 534 535 536 537
        }
        else
        {
            /*
            ** baseURL is empty, assume MRL is absolute
            */
            psz_mrl = CStrFromBSTR(CP_UTF8, _bstr_mrl);
        }
        if( NULL != psz_mrl )
        {
            const char *options[1];
            int i_options = 0;
538

JP Dinger's avatar
JP Dinger committed
539 540 541 542 543
            char timeBuffer[32];
            if( _i_time )
            {
                snprintf(timeBuffer, sizeof(timeBuffer), ":start-time=%d", _i_time);
                options[i_options++] = timeBuffer;
544
            }
JP Dinger's avatar
JP Dinger committed
545
            // add default target to playlist
546
            playlist_add_extended_untrusted(psz_mrl, i_options, options);
JP Dinger's avatar
JP Dinger committed
547
            CoTaskMemFree(psz_mrl);
548
        }
549 550 551
    }
};

552 553 554 555 556 557 558
void VLCPlugin::setErrorInfo(REFIID riid, const char *description)
{
    vlcSupportErrorInfo->setErrorInfo( getClassID() == CLSID_VLCPlugin2 ?
        OLESTR("VideoLAN.VLCPlugin.2") : OLESTR("VideoLAN.VLCPlugin.1"),
        riid, description );
};

559
HRESULT VLCPlugin::onAmbientChanged(LPUNKNOWN pContainer, DISPID dispID)
560
{
561 562
    VARIANT v;
    switch( dispID )
563
    {
564
        case DISPID_AMBIENT_BACKCOLOR:
565 566 567 568 569 570
            VariantInit(&v);
            V_VT(&v) = VT_I4;
            if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
            {
                setBackColor(V_I4(&v));
            }
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
            break;
        case DISPID_AMBIENT_DISPLAYNAME:
            break;
        case DISPID_AMBIENT_FONT:
            break;
        case DISPID_AMBIENT_FORECOLOR:
            break;
        case DISPID_AMBIENT_LOCALEID:
            break;
        case DISPID_AMBIENT_MESSAGEREFLECT:
            break;
        case DISPID_AMBIENT_SCALEUNITS:
            break;
        case DISPID_AMBIENT_TEXTALIGN:
            break;
        case DISPID_AMBIENT_USERMODE:
            VariantInit(&v);
            V_VT(&v) = VT_BOOL;
            if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
            {
                setUserMode(V_BOOL(&v) != VARIANT_FALSE);
            }
            break;
        case DISPID_AMBIENT_UIDEAD:
            break;
        case DISPID_AMBIENT_SHOWGRABHANDLES:
            break;
        case DISPID_AMBIENT_SHOWHATCHING:
            break;
        case DISPID_AMBIENT_DISPLAYASDEFAULT:
            break;
        case DISPID_AMBIENT_SUPPORTSMNEMONICS:
            break;
        case DISPID_AMBIENT_AUTOCLIP:
            break;
        case DISPID_AMBIENT_APPEARANCE:
            break;
        case DISPID_AMBIENT_CODEPAGE:
            VariantInit(&v);
            V_VT(&v) = VT_I4;
            if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
            {
                setCodePage(V_I4(&v));
            }
            break;
        case DISPID_AMBIENT_PALETTE:
            break;
        case DISPID_AMBIENT_CHARSET:
            break;
        case DISPID_AMBIENT_RIGHTTOLEFT:
            break;
        case DISPID_AMBIENT_TOPTOBOTTOM:
            break;
        case DISPID_UNKNOWN:
625 626 627
            /*
            ** multiple property change, look up the ones we are interested in
            */
628 629 630 631 632 633 634 635
            VariantInit(&v);
            V_VT(&v) = VT_BOOL;
            if( SUCCEEDED(GetObjectProperty(pContainer, DISPID_AMBIENT_USERMODE, v)) )
            {
                setUserMode(V_BOOL(&v) != VARIANT_FALSE);
            }
            VariantInit(&v);
            V_VT(&v) = VT_I4;
636
            if( SUCCEEDED(GetObjectProperty(pContainer, DISPID_AMBIENT_CODEPAGE, v)) )
637 638 639 640
            {
                setCodePage(V_I4(&v));
            }
            break;
641 642 643 644
    }
    return S_OK;
};

645 646
HRESULT VLCPlugin::onClose(DWORD dwSaveOption)
{
647 648 649 650 651
    if( isInPlaceActive() )
    {
        onInPlaceDeactivate();
    }
    if( isRunning() )
652
    {
653
        libvlc_instance_t* p_libvlc = _p_libvlc;
654

655
        _p_libvlc = NULL;
656
        vlcDataObject->onClose();
657

658 659
        if( p_libvlc )
            libvlc_release(p_libvlc);
660 661 662 663 664 665 666 667 668 669 670
    }
    return S_OK;
};

BOOL VLCPlugin::isInPlaceActive(void)
{
    return (NULL != _inplacewnd);
};

HRESULT VLCPlugin::onActivateInPlace(LPMSG lpMesg, HWND hwndParent, LPCRECT lprcPosRect, LPCRECT lprcClipRect)
{
Damien Fouilleul's avatar
Damien Fouilleul committed
671
    RECT clipRect = *lprcClipRect;
672

Damien Fouilleul's avatar
Damien Fouilleul committed
673 674
    /*
    ** record keeping of control geometry within container
675
    */
676
    _posRect = *lprcPosRect;
677

Damien Fouilleul's avatar
Damien Fouilleul committed
678 679
    /*
    ** Create a window for in place activated control.
680 681
    ** the window geometry matches the control viewport
    ** within container so that embedded video is always
682
    ** properly displayed.
Damien Fouilleul's avatar
Damien Fouilleul committed
683
    */
684
    _inplacewnd = CreateWindow(_p_class->getInPlaceWndClassName(),
685
            TEXT("VLC Plugin In-Place Window"),
686
            WS_CHILD|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,
687 688 689 690
            lprcPosRect->left,
            lprcPosRect->top,
            lprcPosRect->right-lprcPosRect->left,
            lprcPosRect->bottom-lprcPosRect->top,
691 692 693 694 695 696 697 698 699 700 701
            hwndParent,
            0,
            _p_class->getHInstance(),
            NULL
           );

    if( NULL == _inplacewnd )
        return E_FAIL;

    SetWindowLongPtr(_inplacewnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));

702 703
    /* change cliprect coordinates system relative to window bounding rect */
    OffsetRect(&clipRect, -lprcPosRect->left, -lprcPosRect->top);
704

705 706
    HRGN clipRgn = CreateRectRgnIndirect(&clipRect);
    SetWindowRgn(_inplacewnd, clipRgn, TRUE);
707

708
    if( _b_usermode )
709
    {
710
        /* will run vlc if not done already */
711 712
        libvlc_instance_t* p_libvlc;
        HRESULT result = getVLC(&p_libvlc);
713 714 715
        if( FAILED(result) )
            return result;

716
        if( _b_autoplay && playlist_select(0) )
717
        {
718
            libvlc_media_player_play(_p_mplayer);
JP Dinger's avatar
JP Dinger committed
719
            fireOnPlayEvent();
720
        }
721
    }
722 723 724 725

    if( isVisible() )
        ShowWindow(_inplacewnd, SW_SHOW);

726 727 728 729 730
    return S_OK;
};

HRESULT VLCPlugin::onInPlaceDeactivate(void)
{
731
    if( isPlaying() )
732
    {
733
        playlist_stop();
734 735
        fireOnStopEvent();
    }
736 737 738

    DestroyWindow(_inplacewnd);
    _inplacewnd = NULL;
739

740 741 742 743 744
    return S_OK;
};

void VLCPlugin::setVisible(BOOL fVisible)
{
745 746 747 748 749 750
    if( fVisible != _b_visible )
    {
        _b_visible = fVisible;
        if( isInPlaceActive() )
        {
            ShowWindow(_inplacewnd, fVisible ? SW_SHOW : SW_HIDE);
751
            if( fVisible )
752
                InvalidateRect(_inplacewnd, NULL, TRUE);
753
        }
754
        setDirty(TRUE);
755 756
        firePropChangedEvent(DISPID_Visible);
    }
757 758
};

759 760 761 762 763 764 765 766 767 768 769 770
void VLCPlugin::setVolume(int volume)
{
    if( volume < 0 )
        volume = 0;
    else if( volume > 200 )
        volume = 200;

    if( volume != _i_volume )
    {
        _i_volume = volume;
        if( isRunning() )
        {
771 772 773 774
            libvlc_media_player_t *p_md;
            HRESULT hr = getMD(&p_md);
            if( SUCCEEDED(hr) )
                libvlc_audio_set_volume(p_md, _i_volume);
775 776 777 778 779
        }
        setDirty(TRUE);
    }
};

780 781 782 783 784 785 786 787 788 789 790 791 792
void VLCPlugin::setBackColor(OLE_COLOR backcolor)
{
    if( _i_backcolor != backcolor )
    {
        _i_backcolor = backcolor;
        if( isInPlaceActive() )
        {

        }
        setDirty(TRUE);
    }
};

793 794 795 796 797 798 799
void VLCPlugin::setTime(int seconds)
{
    if( seconds < 0 )
        seconds = 0;

    if( seconds != _i_time )
    {
800
        setStartTime(_i_time);
JP Dinger's avatar
JP Dinger committed
801
        if( NULL != _p_mplayer )
802
        {
803
            libvlc_media_player_set_time(_p_mplayer, _i_time);
804 805 806 807
        }
    }
};

808 809 810 811 812 813 814 815 816 817 818
void VLCPlugin::setFocus(BOOL fFocus)
{
    if( fFocus )
        SetActiveWindow(_inplacewnd);
};

BOOL VLCPlugin::hasFocus(void)
{
    return GetActiveWindow() == _inplacewnd;
};

819 820
void VLCPlugin::onDraw(DVTARGETDEVICE * ptd, HDC hicTargetDev,
        HDC hdcDraw, LPCRECTL lprcBounds, LPCRECTL lprcWBounds)
821
{
822
    if( isVisible() )
823
    {
824 825 826 827
        long width = lprcBounds->right-lprcBounds->left;
        long height = lprcBounds->bottom-lprcBounds->top;

        RECT bounds = { lprcBounds->left, lprcBounds->top, lprcBounds->right, lprcBounds->bottom };
828

829
        if( isUserMode() )
830
        {
831 832 833 834
            /* VLC is in user mode, just draw background color */
            COLORREF colorref = RGB(0, 0, 0);
            OleTranslateColor(_i_backcolor, (HPALETTE)GetStockObject(DEFAULT_PALETTE), &colorref);
            if( colorref != RGB(0, 0, 0) )
835
            {
836 837 838 839
                /* custom background */
                HBRUSH colorbrush = CreateSolidBrush(colorref);
                FillRect(hdcDraw, &bounds, colorbrush);
                DeleteObject((HANDLE)colorbrush);
840
            }
841
            else
842
            {
843 844
                /* black background */
                FillRect(hdcDraw, &bounds, (HBRUSH)GetStockObject(BLACK_BRUSH));
845
            }
846 847 848 849 850
        }
        else
        {
            /* VLC is in design mode, draw the VLC logo */
            FillRect(hdcDraw, &bounds, (HBRUSH)GetStockObject(WHITE_BRUSH));
851

852 853 854 855 856
            LPPICTURE pict = getPicture();
            if( NULL != pict )
            {
                OLE_XSIZE_HIMETRIC picWidth;
                OLE_YSIZE_HIMETRIC picHeight;
857

858 859
                pict->get_Width(&picWidth);
                pict->get_Height(&picHeight);
860

861
                SIZEL picSize = { picWidth, picHeight };
862

863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
                if( NULL != hicTargetDev )
                {
                    DPFromHimetric(hicTargetDev, (LPPOINT)&picSize, 1);
                }
                else if( NULL != (hicTargetDev = CreateDevDC(ptd)) )
                {
                    DPFromHimetric(hicTargetDev, (LPPOINT)&picSize, 1);
                    DeleteDC(hicTargetDev);
                }

                if( picSize.cx > width-4 )
                    picSize.cx = width-4;
                if( picSize.cy > height-4 )
                    picSize.cy = height-4;

                LONG dstX = lprcBounds->left+(width-picSize.cx)/2;
                LONG dstY = lprcBounds->top+(height-picSize.cy)/2;
880

881 882 883 884 885 886
                if( NULL != lprcWBounds )
                {
                    RECT wBounds = { lprcWBounds->left, lprcWBounds->top, lprcWBounds->right, lprcWBounds->bottom };
                    pict->Render(hdcDraw, dstX, dstY, picSize.cx, picSize.cy,
                            0L, picHeight, picWidth, -picHeight, &wBounds);
                }
887
                else
888 889
                    pict->Render(hdcDraw, dstX, dstY, picSize.cx, picSize.cy,
                            0L, picHeight, picWidth, -picHeight, NULL);
890

891 892 893 894 895 896 897 898 899 900 901
                pict->Release();
            }

            SelectObject(hdcDraw, GetStockObject(BLACK_BRUSH));

            MoveToEx(hdcDraw, bounds.left, bounds.top, NULL);
            LineTo(hdcDraw, bounds.left+width-1, bounds.top);
            LineTo(hdcDraw, bounds.left+width-1, bounds.top+height-1);
            LineTo(hdcDraw, bounds.left, bounds.top+height-1);
            LineTo(hdcDraw, bounds.left, bounds.top);
        }
902 903 904 905 906
    }
};

void VLCPlugin::onPaint(HDC hdc, const RECT &bounds, const RECT &clipRect)
{
907
    if( isVisible() )
908
    {
909
        /* if VLC is in design mode, draw control logo */
910 911
        HDC hdcDraw = CreateCompatibleDC(hdc);
        if( NULL != hdcDraw )
912
        {
913 914 915 916
            SIZEL size = getExtent();
            DPFromHimetric(hdc, (LPPOINT)&size, 1);
            RECTL posRect = { 0, 0, size.cx, size.cy };

917 918
            int width = bounds.right-bounds.left;
            int height = bounds.bottom-bounds.top;
919

920 921 922
            HBITMAP hBitmap = CreateCompatibleBitmap(hdc, width, height);
            if( NULL != hBitmap )
            {
923
                HBITMAP oldBmp = (HBITMAP)SelectObject(hdcDraw, hBitmap);
924

925
                if( (size.cx != width) || (size.cy != height) )
926 927 928 929 930 931
                {
                    // needs to scale canvas
                    SetMapMode(hdcDraw, MM_ANISOTROPIC);
                    SetWindowExtEx(hdcDraw, size.cx, size.cy, NULL);
                    SetViewportExtEx(hdcDraw, width, height, NULL);
                }
932

933
                onDraw(NULL, hdc, hdcDraw, &posRect, NULL);
934

935 936 937 938 939
                SetMapMode(hdcDraw, MM_TEXT);
                BitBlt(hdc, bounds.left, bounds.top,
                        width, height,
                        hdcDraw, 0, 0,
                        SRCCOPY);
940

941
                SelectObject(hdcDraw, oldBmp);
942 943
                DeleteObject(hBitmap);
            }
944
            DeleteDC(hdcDraw);
945
        }
946
    }
947 948 949 950
};

void VLCPlugin::onPositionChange(LPCRECT lprcPosRect, LPCRECT lprcClipRect)
{
Damien Fouilleul's avatar
Damien Fouilleul committed
951 952
    RECT clipRect = *lprcClipRect;

953
    //RedrawWindow(GetParent(_inplacewnd), &_posRect, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
954

Damien Fouilleul's avatar
Damien Fouilleul committed
955 956
    /*
    ** record keeping of control geometry within container
957
    */
958
    _posRect = *lprcPosRect;
959 960 961 962

    /*
    ** change in-place window geometry to match clipping region
    */
963
    SetWindowPos(_inplacewnd, NULL,
964 965 966 967
            lprcPosRect->left,
            lprcPosRect->top,
            lprcPosRect->right-lprcPosRect->left,
            lprcPosRect->bottom-lprcPosRect->top,
968 969 970 971
            SWP_NOACTIVATE|
            SWP_NOCOPYBITS|
            SWP_NOZORDER|
            SWP_NOOWNERZORDER );
972

973 974 975
    /* change cliprect coordinates system relative to window bounding rect */
    OffsetRect(&clipRect, -lprcPosRect->left, -lprcPosRect->top);
    HRGN clipRgn = CreateRectRgnIndirect(&clipRect);
976
    SetWindowRgn(_inplacewnd, clipRgn, FALSE);
977

978
    //RedrawWindow(_videownd, &posRect, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
979 980
};

981 982 983 984 985
void VLCPlugin::freezeEvents(BOOL freeze)
{
    vlcConnectionPointContainer->freezeEvents(freeze);
};

986 987
void VLCPlugin::firePropChangedEvent(DISPID dispid)
{
988
    vlcConnectionPointContainer->firePropChangedEvent(dispid);
989 990 991 992
};

void VLCPlugin::fireOnPlayEvent(void)
{
993
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
994
    vlcConnectionPointContainer->fireEvent(DISPID_PlayEvent, &dispparamsNoArgs);
995 996 997 998
};

void VLCPlugin::fireOnPauseEvent(void)
{
999
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1000
    vlcConnectionPointContainer->fireEvent(DISPID_PauseEvent, &dispparamsNoArgs);
1001 1002 1003 1004
};

void VLCPlugin::fireOnStopEvent(void)
{
1005
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1006
    vlcConnectionPointContainer->fireEvent(DISPID_StopEvent, &dispparamsNoArgs);
1007
};
JP Dinger's avatar
JP Dinger committed
1008

1009 1010 1011
/*
 * Async events
 */
1012
void VLCPlugin::fireOnMediaPlayerNothingSpecialEvent()
1013 1014
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1015
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerNothingSpecialEvent, &dispparamsNoArgs);
1016 1017
};

1018
void VLCPlugin::fireOnMediaPlayerOpeningEvent()
1019 1020
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1021
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerOpeningEvent, &dispparamsNoArgs);
1022 1023
};

1024
void VLCPlugin::fireOnMediaPlayerBufferingEvent(long cache)
1025
{
1026 1027 1028 1029 1030 1031 1032 1033 1034
    DISPPARAMS params;
    params.cArgs = 1;
    params.rgvarg = (VARIANTARG *) CoTaskMemAlloc(sizeof(VARIANTARG) * params.cArgs) ;
    memset(params.rgvarg, 0, sizeof(VARIANTARG) * params.cArgs);
    params.rgvarg[0].vt = VT_I4;
    params.rgvarg[0].lVal = cache;
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerBufferingEvent, &params);
1035 1036
};

1037
void VLCPlugin::fireOnMediaPlayerPlayingEvent()
1038 1039
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1040
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerPlayingEvent, &dispparamsNoArgs);
1041 1042
};

1043
void VLCPlugin::fireOnMediaPlayerPausedEvent()
1044 1045
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1046
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerPausedEvent, &dispparamsNoArgs);
1047 1048
};

1049
void VLCPlugin::fireOnMediaPlayerEncounteredErrorEvent()
1050 1051
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1052
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerEncounteredErrorEvent, &dispparamsNoArgs);
1053 1054
};

1055
void VLCPlugin::fireOnMediaPlayerEndReachedEvent()
1056 1057
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1058
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerEndReachedEvent, &dispparamsNoArgs);
1059 1060
};

1061
void VLCPlugin::fireOnMediaPlayerStoppedEvent()
1062 1063
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1064
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerStoppedEvent, &dispparamsNoArgs);
1065 1066
};

1067
void VLCPlugin::fireOnMediaPlayerForwardEvent()
1068 1069
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1070
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerForwardEvent, &dispparamsNoArgs);
1071 1072
};

1073
void VLCPlugin::fireOnMediaPlayerBackwardEvent()
1074 1075
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1076
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerBackwardEvent, &dispparamsNoArgs);
1077 1078 1079 1080 1081 1082 1083 1084
};

static void handle_input_state_event(const libvlc_event_t* event, void *param)
{
    VLCPlugin *plugin = (VLCPlugin*)param;
    switch( event->type )
    {
        case libvlc_MediaPlayerNothingSpecial:
1085
            plugin->fireOnMediaPlayerNothingSpecialEvent();
1086 1087
            break;
        case libvlc_MediaPlayerOpening:
1088
            plugin->fireOnMediaPlayerOpeningEvent();
1089 1090
            break;
        case libvlc_MediaPlayerBuffering:
1091
            plugin->fireOnMediaPlayerBufferingEvent(event->u.media_player_buffering.new_cache);
1092 1093
            break;
        case libvlc_MediaPlayerPlaying:
1094
            plugin->fireOnMediaPlayerPlayingEvent();
1095 1096
            break;
        case libvlc_MediaPlayerPaused:
1097
            plugin->fireOnMediaPlayerPausedEvent();
1098 1099
            break;
        case libvlc_MediaPlayerStopped:
1100
            plugin->fireOnMediaPlayerStoppedEvent();
1101 1102
            break;
        case libvlc_MediaPlayerForward:
1103
            plugin->fireOnMediaPlayerForwardEvent();
1104 1105
            break;
        case libvlc_MediaPlayerBackward:
1106
            plugin->fireOnMediaPlayerBackwardEvent();
1107 1108
            break;
        case libvlc_MediaPlayerEndReached:
1109
            plugin->fireOnMediaPlayerEndReachedEvent();
1110 1111
            break;
        case libvlc_MediaPlayerEncounteredError:
1112
            plugin->fireOnMediaPlayerEncounteredErrorEvent();
1113 1114 1115 1116
            break;
    }
}

1117
void VLCPlugin::fireOnMediaPlayerTimeChangedEvent(long time)
1118
{
1119 1120 1121 1122 1123 1124 1125 1126
    DISPPARAMS params;
    params.cArgs = 1;
    params.rgvarg = (VARIANTARG *) CoTaskMemAlloc(sizeof(VARIANTARG) * params.cArgs) ;
    memset(params.rgvarg, 0, sizeof(VARIANTARG) * params.cArgs);
    params.rgvarg[0].vt = VT_I4;
    params.rgvarg[0].lVal = time;
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
1127
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerTimeChangedEvent, &params);
1128 1129 1130 1131 1132
};

static void handle_time_changed_event(const libvlc_event_t* event, void *param)
{
    VLCPlugin *plugin = (VLCPlugin*)param;
1133
    plugin->fireOnMediaPlayerTimeChangedEvent(event->u.media_player_time_changed.new_time);
1134 1135
}

1136
void VLCPlugin::fireOnMediaPlayerPositionChangedEvent(long position)
1137
{
1138 1139 1140 1141 1142 1143 1144 1145
    DISPPARAMS params;
    params.cArgs = 1;
    params.rgvarg = (VARIANTARG *) CoTaskMemAlloc(sizeof(VARIANTARG) * params.cArgs) ;
    memset(params.rgvarg, 0, sizeof(VARIANTARG) * params.cArgs);
    params.rgvarg[0].vt = VT_I4;
    params.rgvarg[0].lVal = position;
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
1146
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerPositionChangedEvent, &params);
1147 1148 1149 1150 1151
};

static void handle_position_changed_event(const libvlc_event_t* event, void *param)
{
    VLCPlugin *plugin = (VLCPlugin*)param;
1152
    plugin->fireOnMediaPlayerPositionChangedEvent(event->u.media_player_position_changed.new_position);
1153 1154
}

1155
#define B(val) ((val) ? 0xFFFF : 0x0000)
1156
void VLCPlugin::fireOnMediaPlayerSeekableChangedEvent(VARIANT_BOOL seekable)
1157
{
1158 1159 1160 1161 1162 1163 1164 1165
    DISPPARAMS params;
    params.cArgs = 1;
    params.rgvarg = (VARIANTARG *) CoTaskMemAlloc(sizeof(VARIANTARG) * params.cArgs) ;
    memset(params.rgvarg, 0, sizeof(VARIANTARG) * params.cArgs);
    params.rgvarg[0].vt = VT_BOOL;
    params.rgvarg[0].boolVal = seekable;
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
1166
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerSeekableChangedEvent, &params);
1167 1168 1169 1170 1171
};

static void handle_seekable_changed_event(const libvlc_event_t* event, void *param)
{
    VLCPlugin *plugin = (VLCPlugin*)param;
1172
    plugin->fireOnMediaPlayerSeekableChangedEvent(B(event->u.media_player_seekable_changed.new_seekable));
1173 1174
}

1175
void VLCPlugin::fireOnMediaPlayerPausableChangedEvent(VARIANT_BOOL pausable)
1176
{
1177 1178 1179 1180 1181 1182 1183 1184
    DISPPARAMS params;
    params.cArgs = 1;
    params.rgvarg = (VARIANTARG *) CoTaskMemAlloc(sizeof(VARIANTARG) * params.cArgs) ;
    memset(params.rgvarg, 0, sizeof(VARIANTARG) * params.cArgs);
    params.rgvarg[0].vt = VT_BOOL;
    params.rgvarg[0].boolVal = pausable;
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
1185
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerPausableChangedEvent, &params);
1186 1187 1188 1189 1190
};

static void handle_pausable_changed_event(const libvlc_event_t* event, void *param)
{
    VLCPlugin *plugin = (VLCPlugin*)param;
1191
    plugin->fireOnMediaPlayerPausableChangedEvent(B(event->u.media_player_pausable_changed.new_pausable));
1192
}
1193
#undef B
1194 1195 1196

/* */

1197
bool VLCPlugin::playlist_select( int idx )
JP Dinger's avatar
JP Dinger committed
1198 1199 1200
{
    libvlc_media_t *p_m = NULL;

1201 1202
    assert(_p_mlist);

JP Dinger's avatar
JP Dinger committed
1203 1204
    libvlc_media_list_lock(_p_mlist);

1205
    int count = libvlc_media_list_count(_p_mlist);
JP Dinger's avatar
JP Dinger committed
1206

1207
    if( (idx < 0) || (idx >= count) )
JP Dinger's avatar
JP Dinger committed
1208 1209 1210 1211
        goto bad_unlock;

    _i_midx = idx;

1212
    p_m = libvlc_media_list_item_at_index(_p_mlist,_i_midx);
JP Dinger's avatar
JP Dinger committed
1213
    libvlc_media_list_unlock(_p_mlist);
1214
    if( !p_m )
JP Dinger's avatar
JP Dinger committed
1215 1216 1217 1218
        return false;

    if( _p_mplayer )
    {
1219 1220
        if( isPlaying() )
            playlist_stop();
1221
        player_unregister_events();
JP Dinger's avatar
JP Dinger committed
1222 1223 1224 1225
        libvlc_media_player_release( _p_mplayer );
        _p_mplayer = NULL;
    }

1226
    _p_mplayer = libvlc_media_player_new_from_media(p_m);
JP Dinger's avatar
JP Dinger committed
1227
    if( _p_mplayer )
1228 1229 1230 1231 1232 1233
    {
        // initial volume setting
        libvlc_audio_set_volume(_p_mplayer, _i_volume);
        if( _b_mute )
            libvlc_audio_set_mute(_p_mplayer, TRUE);
        set_player_window();
1234
        player_register_events();
1235
    }
JP Dinger's avatar
JP Dinger committed
1236

1237 1238
    libvlc_media_release(p_m);
    return _p_mplayer ? true : false;
JP Dinger's avatar
JP Dinger committed
1239 1240 1241 1242 1243 1244

bad_unlock:
    libvlc_media_list_unlock(_p_mlist);
    return false;
}

1245
void VLCPlugin::set_player_window()
JP Dinger's avatar
JP Dinger committed
1246 1247
{
    // XXX FIXME no idea if this is correct or not
1248
    libvlc_media_player_set_hwnd(_p_mplayer,getInPlaceWindow());
JP Dinger's avatar
JP Dinger committed
1249 1250
}

1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
void VLCPlugin::player_register_events()
{
    libvlc_event_manager_t *eventManager = NULL;
    assert(_p_mplayer);

    eventManager = libvlc_media_player_event_manager(_p_mplayer);
    if(eventManager) {
        libvlc_event_attach(eventManager, libvlc_MediaPlayerNothingSpecial,
                            handle_input_state_event, this);
        libvlc_event_attach(eventManager, libvlc_MediaPlayerOpening,
                            handle_input_state_event, this);
        libvlc_event_attach(eventManager, libvlc_MediaPlayerBuffering,
                            handle_input_state_event, this);
        libvlc_event_attach(eventManager, libvlc_MediaPlayerPlaying,
                            handle_input_state_event, this);
        libvlc_event_attach(eventManager, libvlc_MediaPlayerPaused,
                            handle_input_state_event, this);
        libvlc_event_attach(eventManager, libvlc_MediaPlayerStopped,
                            handle_input_state_event, this);
        libvlc_event_attach(eventManager, libvlc_MediaPlayerForward,
                            handle_input_state_event, this);
        libvlc_event_attach(eventManager, libvlc_MediaPlayerBackward,
                            handle_input_state_event, this);
        libvlc_event_attach(eventManager, libvlc_MediaPlayerEndReached,
                            handle_input_state_event, this);
        libvlc_event_attach(eventManager, libvlc_MediaPlayerEncounteredError,
                            handle_input_state_event, this);

        libvlc_event_attach(eventManager, libvlc_MediaPlayerTimeChanged,
                            handle_time_changed_event, this);
        libvlc_event_attach(eventManager, libvlc_MediaPlayerPositionChanged,
                            handle_position_changed_event, this);
        libvlc_event_attach(eventManager, libvlc_MediaPlayerSeekableChanged,
                            handle_seekable_changed_event, this);
        libvlc_event_attach(eventManager, libvlc_MediaPlayerPausableChanged,
                            handle_pausable_changed_event, this);
    }
}

void VLCPlugin::player_unregister_events()
{
    libvlc_event_manager_t *eventManager = NULL;
    assert(_p_mplayer);

    eventManager = libvlc_media_player_event_manager(_p_mplayer);
    if(eventManager) {
        libvlc_event_detach(eventManager, libvlc_MediaPlayerNothingSpecial,
                            handle_input_state_event, this);
        libvlc_event_detach(eventManager, libvlc_MediaPlayerOpening,
                            handle_input_state_event, this);
        libvlc_event_detach(eventManager, libvlc_MediaPlayerBuffering,
                            handle_input_state_event, this);
        libvlc_event_detach(eventManager, libvlc_MediaPlayerPlaying,
                            handle_input_state_event, this);
        libvlc_event_detach(eventManager, libvlc_MediaPlayerPaused,
                            handle_input_state_event, this);
        libvlc_event_detach(eventManager, libvlc_MediaPlayerStopped,
                            handle_input_state_event, this);
        libvlc_event_detach(eventManager, libvlc_MediaPlayerForward,
                            handle_input_state_event, this);
        libvlc_event_detach(eventManager, libvlc_MediaPlayerBackward,
                            handle_input_state_event, this);
        libvlc_event_detach(eventManager, libvlc_MediaPlayerEndReached,
                            handle_input_state_event, this);
        libvlc_event_detach(eventManager, libvlc_MediaPlayerEncounteredError,
                            handle_input_state_event, this);

        libvlc_event_detach(eventManager, libvlc_MediaPlayerTimeChanged,
                            handle_time_changed_event, this);
        libvlc_event_detach(eventManager, libvlc_MediaPlayerPositionChanged,
                            handle_position_changed_event, this);
        libvlc_event_detach(eventManager, libvlc_MediaPlayerSeekableChanged,
                            handle_seekable_changed_event, this);
        libvlc_event_detach(eventManager, libvlc_MediaPlayerPausableChanged,
                            handle_pausable_changed_event, this);
    }
}

1329
int  VLCPlugin::playlist_add_extended_untrusted(const char *mrl, int optc, const char **optv)
JP Dinger's avatar
JP Dinger committed
1330 1331
{
    int item = -1;
1332
    libvlc_media_t *p_m = libvlc_media_new_location(_p_libvlc,mrl);
1333
    if( !p_m )
JP Dinger's avatar
JP Dinger committed
1334 1335 1336
        return -1;

    for( int i = 0; i < optc; ++i )
1337
        libvlc_media_add_option_flag(p_m, optv[i], libvlc_media_option_unique);
JP Dinger's avatar
JP Dinger committed
1338 1339

    libvlc_media_list_lock(_p_mlist);
1340
    if( libvlc_media_list_add_media(_p_mlist,p_m) == 0 )
1341
        item = libvlc_media_list_count(_p_mlist)-1;
JP Dinger's avatar
JP Dinger committed
1342 1343 1344 1345 1346
    libvlc_media_list_unlock(_p_mlist);
    libvlc_media_release(p_m);

    return item;
}