plugin.cpp 44.1 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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
27

28 29 30 31 32 33 34 35 36 37 38 39
#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"
40
#include "vlccontrol2.h"
41
#include "viewobject.h"
42
#include "dataobject.h"
43
#include "supporterrorinfo.h"
44 45 46

#include "utils.h"

47
#include <stdio.h>
48 49
#include <string.h>
#include <winreg.h>
50 51 52 53
#include <winuser.h>
#include <servprov.h>
#include <shlwapi.h>
#include <wininet.h>
54
#include <windowsx.h>
55 56 57

using namespace std;

58 59 60 61 62 63 64 65
#define LEFT_BUTTON     0x01
#define RIGHT_BUTTON    0x02
#define MIDDLE_BUTTON   0x04

#define SHIFT_MASK      0x01
#define CTRL_MASK       0x02
#define ALT_MASK        0x04

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
////////////////////////////////////////////////////////////////////////
//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) )
            {
82 83
                RECT bounds;
                GetClientRect(hWnd, &bounds);
84
                BeginPaint(hWnd, &ps);
85
                p_instance->onPaint(ps.hdc, bounds, pr);
86 87 88
                EndPaint(hWnd, &ps);
            }
            return 0L;
89 90 91 92 93 94 95 96 97 98
        case WM_SIZE:{
            int new_client_width = LOWORD(lParam);
            int new_client_height = HIWORD(lParam);
            //first child will be resized to client area
            HWND hChildWnd = GetWindow(hWnd, GW_CHILD);
            if(hChildWnd){
                MoveWindow(hChildWnd, 0, 0, new_client_width, new_client_height, FALSE);
            }
            return 0L;
        }
99 100 101 102 103 104
        case WM_CHAR:
        case WM_SYSKEYUP:
        case WM_KEYUP:
        case WM_SYSKEYDOWN:
        case WM_KEYDOWN:
            p_instance->OnKeyEvent(uMsg, wParam, lParam);
105
    }
106
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
107 108
};

109
VLCPluginClass::VLCPluginClass(LONG *p_class_ref, HINSTANCE hInstance, REFCLSID rclsid) :
110
    _p_class_ref(p_class_ref),
111
    _class_ref(0),
112
    _hinstance(hInstance),
113
    _classid(rclsid),
114
    _inplace_picture(NULL)
115 116 117 118 119
{
    WNDCLASS wClass;

    if( ! GetClassInfo(hInstance, getInPlaceWndClassName(), &wClass) )
    {
120
        wClass.style          = CS_NOCLOSE|CS_DBLCLKS;
121 122 123 124 125 126 127 128 129
        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();
130

131 132 133 134 135 136 137
        _inplace_wndclass_atom = RegisterClass(&wClass);
    }
    else
    {
        _inplace_wndclass_atom = 0;
    }

138
    HBITMAP hbitmap = (HBITMAP)LoadImage(getHInstance(), MAKEINTRESOURCE(2), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
139 140 141 142 143 144 145 146
    if( NULL != hbitmap )
    {
        PICTDESC pictDesc;

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

148 149 150
        if( FAILED(OleCreatePictureIndirect(&pictDesc, IID_IPicture, TRUE, reinterpret_cast<LPVOID*>(&_inplace_picture))) )
            _inplace_picture = NULL;
    }
151 152 153 154 155 156 157 158
    AddRef();
};

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

159 160
    if( NULL != _inplace_picture )
        _inplace_picture->Release();
161 162 163 164 165 166 167
};

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

168 169
    if( (IID_IUnknown == riid)
     || (IID_IClassFactory == riid) )
170 171 172 173 174 175 176 177 178 179 180 181 182 183
    {
        AddRef();
        *ppv = reinterpret_cast<LPVOID>(this);

        return NOERROR;
    }

    *ppv = NULL;

    return E_NOINTERFACE;
};

STDMETHODIMP_(ULONG) VLCPluginClass::AddRef(void)
{
184 185
    InterlockedIncrement(_p_class_ref);
    return ++_class_ref;
186 187 188 189
};

STDMETHODIMP_(ULONG) VLCPluginClass::Release(void)
{
190 191
    InterlockedDecrement(_p_class_ref);
    if( 0 == (--_class_ref) )
192 193 194 195
    {
        delete this;
        return 0;
    }
196
    return _class_ref;
197 198
};

199
STDMETHODIMP VLCPluginClass::CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, void **ppv)
200 201 202 203 204 205
{
    if( NULL == ppv )
        return E_POINTER;

    *ppv = NULL;

206
    if( (NULL != pUnkOuter) && (IID_IUnknown != riid) ) {
207
        return CLASS_E_NOAGGREGATION;
208
    }
209

210
    VLCPlugin *plugin = new VLCPlugin(this, pUnkOuter);
211 212 213
    if( NULL != plugin )
    {
        HRESULT hr = plugin->QueryInterface(riid, ppv);
214
        // the following will destroy the object if QueryInterface() failed
215 216 217 218 219 220 221 222 223 224
        plugin->Release();
        return hr;
    }
    return E_OUTOFMEMORY;
};

STDMETHODIMP VLCPluginClass::LockServer(BOOL fLock)
{
    if( fLock )
        AddRef();
225
    else
226 227 228 229 230 231
        Release();

    return S_OK;
};

////////////////////////////////////////////////////////////////////////
232
extern HMODULE DllGetModule();
233

234
VLCPlugin::VLCPlugin(VLCPluginClass *p_class, LPUNKNOWN pUnkOuter) :
235
    _inplacewnd(NULL),
236
    _WindowsManager(DllGetModule(), _ViewRC, &m_player, this, this),
237 238
    _p_class(p_class),
    _i_ref(1UL),
239
    _i_codepage(CP_ACP),
Rafaël Carré's avatar
Rafaël Carré committed
240
    _b_usermode(TRUE)
241
{
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    _ViewRC.hDeFullscreenBitmap =
        LoadImage(DllGetModule(), MAKEINTRESOURCE(3),
                  IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS);

    _ViewRC.hPlayBitmap =
        LoadImage(DllGetModule(), MAKEINTRESOURCE(4),
                  IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS);

    _ViewRC.hPauseBitmap =
        LoadImage(DllGetModule(), MAKEINTRESOURCE(5),
                  IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS);

    _ViewRC.hVolumeBitmap =
        LoadImage(DllGetModule(), MAKEINTRESOURCE(6),
                  IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS);

    _ViewRC.hVolumeMutedBitmap =
        LoadImage(DllGetModule(), MAKEINTRESOURCE(7),
                  IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS);

    _ViewRC.hBackgroundIcon =
        (HICON) LoadImage(DllGetModule(), MAKEINTRESOURCE(8),
                          IMAGE_ICON, 0, 0, LR_DEFAULTSIZE);

    _ViewRC.hFullscreenBitmap =
        LoadImage(DllGetModule(), MAKEINTRESOURCE(9),
                  IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS);

270 271 272 273 274 275 276 277 278 279 280
    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);
281
    vlcControl2 = new VLCControl2(this);
282
    vlcViewObject = new VLCViewObject(this);
283
    vlcDataObject = new VLCDataObject(this);
284
    vlcOleObject = new VLCOleObject(this);
285
    vlcSupportErrorInfo = new VLCSupportErrorInfo(this);
286 287 288 289 290

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

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

293 294
    // make sure that persistable properties are initialized
    onInit();
295 296 297

    initVLC();
}
298 299 300

VLCPlugin::~VLCPlugin()
{
301
    delete vlcSupportErrorInfo;
302
    delete vlcOleObject;
303
    delete vlcDataObject;
304
    delete vlcViewObject;
305
    delete vlcControl2;
306 307 308 309 310 311 312
    delete vlcConnectionPointContainer;
    delete vlcProvideClassInfo;
    delete vlcPersistPropertyBag;
    delete vlcPersistStreamInit;
    delete vlcPersistStorage;
    delete vlcOleInPlaceActiveObject;
    delete vlcOleInPlaceObject;
313
    delete vlcObjectSafety;
314

315 316 317
    delete vlcOleControl;
    if( _p_pict )
        _p_pict->Release();
318

319
    SysFreeString(_bstr_mrl);
320
    SysFreeString(_bstr_baseurl);
321 322

    _p_class->Release();
323
}
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

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 )
343
        *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
344 345 346 347 348 349 350 351 352 353 354 355
    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);
356
    else if (IID_IObjectSafety == riid)
357
        *ppv = reinterpret_cast<LPVOID>(vlcObjectSafety);
358
    else if( IID_IVLCControl2 == riid || IID_IDispatch == riid)
359
        *ppv = reinterpret_cast<LPVOID>(vlcControl2);
360 361
    else if( IID_IViewObject == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
Damien Fouilleul's avatar
Damien Fouilleul committed
362 363
    else if( IID_IViewObject2 == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
364 365
    else if( IID_IDataObject == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcDataObject);
366 367
    else if( IID_ISupportErrorInfo == riid )
        *ppv = reinterpret_cast<LPVOID>(vlcSupportErrorInfo);
368 369 370 371
    else
    {
        *ppv = NULL;
        return E_NOINTERFACE;
Damien Fouilleul's avatar
Damien Fouilleul committed
372
    }
373 374
    ((LPUNKNOWN)*ppv)->AddRef();
    return NOERROR;
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
};

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;
390
}
391 392 393

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

394
HRESULT VLCPlugin::onInit(void)
395
{
396 397 398 399 400 401 402
    // initialize persistable properties
    set_autoplay(true);
    _b_autoloop   = FALSE;
    _bstr_baseurl = NULL;
    _bstr_mrl     = NULL;
    _b_visible    = TRUE;
    _b_mute       = FALSE;
403
    _i_volume     = 100;
404 405 406 407 408 409 410 411 412 413
    _i_time       = 0;
    _i_backcolor  = 0;
    // 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);
    return S_OK;
}
414 415 416

HRESULT VLCPlugin::onLoad(void)
{
417
    if( SysStringLen(_bstr_baseurl) == 0 )
418
    {
419
        /*
420
        ** try to retreive the base URL using the client site moniker, which for Internet Explorer
421
        ** is the URL of the page the plugin is embedded into.
422
        */
423
        LPOLECLIENTSITE pClientSite;
424
        if( SUCCEEDED(vlcOleObject->GetClientSite(&pClientSite)) && (NULL != pClientSite) )
425 426 427 428 429 430 431 432
        {
            IBindCtx *pBC = 0;
            if( SUCCEEDED(CreateBindCtx(0, &pBC)) )
            {
                LPMONIKER pContMoniker = NULL;
                if( SUCCEEDED(pClientSite->GetMoniker(OLEGETMONIKER_ONLYIFTHERE,
                                OLEWHICHMK_CONTAINER, &pContMoniker)) )
                {
433 434
                    LPOLESTR base_url;
                    if( SUCCEEDED(pContMoniker->GetDisplayName(pBC, NULL, &base_url)) )
435
                    {
436 437 438
                        /*
                        ** check that the moniker name is a URL
                        */
439
                        if( UrlIsW(base_url, URLIS_URL) )
440
                        {
441
                            /* copy base URL */
442
                            _bstr_baseurl = SysAllocString(base_url);
443
                        }
444
                        CoTaskMemFree(base_url);
445 446 447 448
                    }
                }
            }
        }
449 450 451
    }
    setDirty(FALSE);
    return S_OK;
452
}
453

JP Dinger's avatar
JP Dinger committed
454
void VLCPlugin::initVLC()
455
{
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
    try
    {
        static const char * const ppsz_argv[] = {
            "-vv",
            "--no-stats",
            "--intf=dummy",
            "--no-video-title-show",
        };

        auto instance = VLC::Instance( sizeof(ppsz_argv) / sizeof(*ppsz_argv), ppsz_argv );
        if( !m_player.open( instance ) )
            return;
    }
    catch (std::runtime_error&)
    {
471
        return;
472
    }
473

474 475 476
    // register player events
    player_register_events();

477 478 479 480 481 482 483 484 485
    if( !isInPlaceActive()  )
    {
        LPOLECLIENTSITE pClientSite;
        if( SUCCEEDED(vlcOleObject->GetClientSite(&pClientSite)) && (NULL != pClientSite) )
        {
            vlcOleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, pClientSite, 0, NULL, NULL);
            pClientSite->Release();
        }
    }
486 487
};

488 489 490 491 492 493 494
void VLCPlugin::setErrorInfo(REFIID riid, const char *description)
{
    vlcSupportErrorInfo->setErrorInfo( getClassID() == CLSID_VLCPlugin2 ?
        OLESTR("VideoLAN.VLCPlugin.2") : OLESTR("VideoLAN.VLCPlugin.1"),
        riid, description );
};

495
HRESULT VLCPlugin::onAmbientChanged(LPUNKNOWN pContainer, DISPID dispID)
496
{
497 498
    VARIANT v;
    switch( dispID )
499
    {
500
        case DISPID_AMBIENT_BACKCOLOR:
501 502 503 504 505 506
            VariantInit(&v);
            V_VT(&v) = VT_I4;
            if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
            {
                setBackColor(V_I4(&v));
            }
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
            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:
561 562 563
            /*
            ** multiple property change, look up the ones we are interested in
            */
564 565 566 567 568 569 570 571
            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;
572
            if( SUCCEEDED(GetObjectProperty(pContainer, DISPID_AMBIENT_CODEPAGE, v)) )
573 574 575 576
            {
                setCodePage(V_I4(&v));
            }
            break;
577 578
    }
    return S_OK;
579
}
580

581
HRESULT VLCPlugin::onClose(DWORD)
582
{
583 584 585 586
    if( isInPlaceActive() )
    {
        onInPlaceDeactivate();
    }
587
    vlcDataObject->onClose();
588
    return S_OK;
589
}
590 591 592 593

BOOL VLCPlugin::isInPlaceActive(void)
{
    return (NULL != _inplacewnd);
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 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
static short _shiftState()
{
    short shift = (GetKeyState(VK_SHIFT) < 0) ? SHIFT_MASK : 0;
    short ctrl  = (GetKeyState(VK_CONTROL) < 0) ? CTRL_MASK : 0;
    short alt   = (GetKeyState(VK_MENU) < 0) ? ALT_MASK : 0;
    return (shift | ctrl | alt);
}

void VLCPlugin::OnKeyEvent(UINT uKeyMsg, WPARAM wParam, LPARAM lParam)
{
    USHORT nChar = (USHORT)wParam;
    USHORT nShiftState = _shiftState();
    switch (uKeyMsg) {
        case WM_SYSKEYDOWN:
        case WM_KEYDOWN:
            fireKeyDownEvent(nChar , nShiftState);
            break;
        case WM_CHAR:
            fireKeyPressEvent(nChar);
            break;
        case WM_SYSKEYUP:
        case WM_KEYUP:
            fireKeyUpEvent(nChar , nShiftState);
            break;
    }
}

void VLCPlugin::OnMouseEvent(UINT uMouseMsg, WPARAM wParam, LPARAM lParam)
{
    short state = _shiftState();
    int x = GET_X_LPARAM(lParam);
    int y = GET_Y_LPARAM(lParam);
    switch (uMouseMsg) {
        case WM_MOUSEMOVE:
        {
            short button = 0;
            button |= (wParam & MK_LBUTTON) ? LEFT_BUTTON : 0;
            button |= (wParam & MK_MBUTTON) ? MIDDLE_BUTTON : 0;
            button |= (wParam & MK_RBUTTON) ? RIGHT_BUTTON : 0;
            fireMouseMoveEvent( button, state, x, y );
            break;
        }

        case WM_RBUTTONDBLCLK:
        case WM_MBUTTONDBLCLK:
        case WM_LBUTTONDBLCLK:
            fireDblClickEvent();
            break;

        case WM_RBUTTONDOWN:
            fireMouseDownEvent( RIGHT_BUTTON, state , x, y );
            break;
        case WM_MBUTTONDOWN:
            fireMouseDownEvent( MIDDLE_BUTTON, state , x, y );
            break;
        case WM_LBUTTONDOWN:
            fireMouseDownEvent( LEFT_BUTTON, state , x, y );
            break;

        case WM_RBUTTONUP:
            fireMouseUpEvent( RIGHT_BUTTON, state , x, y );
            fireClickEvent();
            break;
        case WM_MBUTTONUP:
            fireMouseUpEvent( MIDDLE_BUTTON, state , x, y );
            fireClickEvent();
            break;
        case WM_LBUTTONUP:
            fireMouseUpEvent( LEFT_BUTTON, state , x, y );
            fireClickEvent();
            break;

        default:
            break;
    }
}

673
HRESULT VLCPlugin::onActivateInPlace(LPMSG, HWND hwndParent, LPCRECT lprcPosRect, LPCRECT lprcClipRect)
674
{
Damien Fouilleul's avatar
Damien Fouilleul committed
675
    RECT clipRect = *lprcClipRect;
676

Damien Fouilleul's avatar
Damien Fouilleul committed
677 678
    /*
    ** record keeping of control geometry within container
679
    */
680
    _posRect = *lprcPosRect;
681

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

    if( NULL == _inplacewnd )
        return E_FAIL;

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

706 707
    /* change cliprect coordinates system relative to window bounding rect */
    OffsetRect(&clipRect, -lprcPosRect->left, -lprcPosRect->top);
708

709 710
    HRGN clipRgn = CreateRectRgnIndirect(&clipRect);
    SetWindowRgn(_inplacewnd, clipRgn, TRUE);
711

712 713
    if( isUserMode() )
    {
714
        _WindowsManager.CreateWindows(this->getInPlaceWindow());
715

716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
        // 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 )
                {
                    psz_mrl = CStrFromWSTR(CP_UTF8, abs_url, wcslen(abs_url));
                    CoTaskMemFree(abs_url);
                }
                else
                {
                    psz_mrl = CStrFromBSTR(CP_UTF8, _bstr_mrl);
                }
            }
            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;

                char timeBuffer[32];
                if( _i_time )
                {
                    snprintf(timeBuffer, sizeof(timeBuffer), ":start-time=%d", _i_time);
                    options[i_options++] = timeBuffer;
                }
                // add default target to playlist
                m_player.add_item( psz_mrl, i_options, options);
                CoTaskMemFree(psz_mrl);
            }
        }

761
        if( get_autoplay() )
762
        {
763
            get_player().play();
764
        }
765
    }
766 767 768 769

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

770 771
    set_player_window();

772 773 774
    return S_OK;
};

775 776 777 778 779
void VLCPlugin::toggleFullscreen()
{
    _WindowsManager.ToggleFullScreen();
}

780 781 782 783 784 785 786 787 788 789 790 791 792
void VLCPlugin::setFullscreen(BOOL yes)
{
    if( yes )
        _WindowsManager.StartFullScreen();
    else
        _WindowsManager.EndFullScreen();
}

BOOL VLCPlugin::getFullscreen()
{
    return _WindowsManager.IsFullScreen();
}

793 794
HRESULT VLCPlugin::onInPlaceDeactivate(void)
{
795
    if( m_player.mlp().isPlaying() )
796 797 798
    {
        m_player.mlp().stopAsync();
    }
799

800 801
    _WindowsManager.DestroyWindows();

802 803
    DestroyWindow(_inplacewnd);
    _inplacewnd = NULL;
804

805 806 807 808 809
    return S_OK;
};

void VLCPlugin::setVisible(BOOL fVisible)
{
810 811 812 813 814 815
    if( fVisible != _b_visible )
    {
        _b_visible = fVisible;
        if( isInPlaceActive() )
        {
            ShowWindow(_inplacewnd, fVisible ? SW_SHOW : SW_HIDE);
816
            if( fVisible )
817
                InvalidateRect(_inplacewnd, NULL, TRUE);
818
        }
819
        setDirty(TRUE);
820 821
        firePropChangedEvent(DISPID_Visible);
    }
822 823
};

824 825 826 827 828 829 830 831 832 833
void VLCPlugin::setVolume(int volume)
{
    if( volume < 0 )
        volume = 0;
    else if( volume > 200 )
        volume = 200;

    if( volume != _i_volume )
    {
        _i_volume = volume;
834 835
        if ( m_player.get_mp().setVolume( volume ) )
            setDirty(TRUE);
836
    }
837
}
838

839 840 841 842 843 844 845 846 847 848 849 850 851
void VLCPlugin::setBackColor(OLE_COLOR backcolor)
{
    if( _i_backcolor != backcolor )
    {
        _i_backcolor = backcolor;
        if( isInPlaceActive() )
        {

        }
        setDirty(TRUE);
    }
};

852 853 854 855 856 857 858
void VLCPlugin::setTime(int seconds)
{
    if( seconds < 0 )
        seconds = 0;

    if( seconds != _i_time )
    {
859
        setStartTime(_i_time);
860
        m_player.get_mp().setTime( _i_time, true );
861
    }
862
}
863

864 865 866 867 868 869 870 871 872
void VLCPlugin::setMute(BOOL mute)
{
    if( mute != _b_mute )
    {
        _b_mute = mute;
        m_player.get_mp().setMute( _b_mute );
    }
}

873 874 875 876 877 878 879 880 881 882 883
void VLCPlugin::setFocus(BOOL fFocus)
{
    if( fFocus )
        SetActiveWindow(_inplacewnd);
};

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

884 885
void VLCPlugin::onDraw(DVTARGETDEVICE * ptd, HDC hicTargetDev,
        HDC hdcDraw, LPCRECTL lprcBounds, LPCRECTL lprcWBounds)
886
{
887
    if( isVisible() )
888
    {
889 890 891 892
        long width = lprcBounds->right-lprcBounds->left;
        long height = lprcBounds->bottom-lprcBounds->top;

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

894
        if( isUserMode() )
895
        {
896 897 898 899
            /* 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) )
900
            {
901 902 903 904
                /* custom background */
                HBRUSH colorbrush = CreateSolidBrush(colorref);
                FillRect(hdcDraw, &bounds, colorbrush);
                DeleteObject((HANDLE)colorbrush);
905
            }
906
            else
907
            {
908 909
                /* black background */
                FillRect(hdcDraw, &bounds, (HBRUSH)GetStockObject(BLACK_BRUSH));
910
            }
911 912 913 914 915
        }
        else
        {
            /* VLC is in design mode, draw the VLC logo */
            FillRect(hdcDraw, &bounds, (HBRUSH)GetStockObject(WHITE_BRUSH));
916

917 918 919 920 921
            LPPICTURE pict = getPicture();
            if( NULL != pict )
            {
                OLE_XSIZE_HIMETRIC picWidth;
                OLE_YSIZE_HIMETRIC picHeight;
922

923 924
                pict->get_Width(&picWidth);
                pict->get_Height(&picHeight);
925

926
                SIZEL picSize = { picWidth, picHeight };
927

928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
                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;
945

946 947 948 949 950 951
                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);
                }
952
                else
953 954
                    pict->Render(hdcDraw, dstX, dstY, picSize.cx, picSize.cy,
                            0L, picHeight, picWidth, -picHeight, NULL);
955

956 957 958 959 960 961 962 963 964 965 966
                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);
        }
967 968 969
    }
};

970
void VLCPlugin::onPaint(HDC hdc, const RECT &bounds, const RECT &)
971
{
972
    if( isVisible() )
973
    {
974
        /* if VLC is in design mode, draw control logo */
975 976
        HDC hdcDraw = CreateCompatibleDC(hdc);
        if( NULL != hdcDraw )
977
        {
978 979 980 981
            SIZEL size = getExtent();
            DPFromHimetric(hdc, (LPPOINT)&size, 1);
            RECTL posRect = { 0, 0, size.cx, size.cy };

982 983
            int width = bounds.right-bounds.left;
            int height = bounds.bottom-bounds.top;
984

985 986 987
            HBITMAP hBitmap = CreateCompatibleBitmap(hdc, width, height);
            if( NULL != hBitmap )
            {
988
                HBITMAP oldBmp = (HBITMAP)SelectObject(hdcDraw, hBitmap);
989

990
                if( (size.cx != width) || (size.cy != height) )
991 992 993 994 995 996
                {
                    // needs to scale canvas
                    SetMapMode(hdcDraw, MM_ANISOTROPIC);
                    SetWindowExtEx(hdcDraw, size.cx, size.cy, NULL);
                    SetViewportExtEx(hdcDraw, width, height, NULL);
                }
997

998
                onDraw(NULL, hdc, hdcDraw, &posRect, NULL);
999

1000 1001 1002 1003 1004
                SetMapMode(hdcDraw, MM_TEXT);
                BitBlt(hdc, bounds.left, bounds.top,
                        width, height,
                        hdcDraw, 0, 0,
                        SRCCOPY);
1005

1006
                SelectObject(hdcDraw, oldBmp);
1007 1008
                DeleteObject(hBitmap);
            }
1009
            DeleteDC(hdcDraw);
1010
        }
1011
    }
1012 1013 1014 1015
};

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

1018
    //RedrawWindow(GetParent(_inplacewnd), &_posRect, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
1019

Damien Fouilleul's avatar
Damien Fouilleul committed
1020 1021
    /*
    ** record keeping of control geometry within container
1022
    */
1023
    _posRect = *lprcPosRect;
1024 1025 1026 1027

    /*
    ** change in-place window geometry to match clipping region
    */
1028
    SetWindowPos(_inplacewnd, NULL,
1029 1030 1031 1032
            lprcPosRect->left,
            lprcPosRect->top,
            lprcPosRect->right-lprcPosRect->left,
            lprcPosRect->bottom-lprcPosRect->top,
1033 1034 1035 1036
            SWP_NOACTIVATE|
            SWP_NOCOPYBITS|
            SWP_NOZORDER|
            SWP_NOOWNERZORDER );
1037

1038 1039 1040
    /* change cliprect coordinates system relative to window bounding rect */
    OffsetRect(&clipRect, -lprcPosRect->left, -lprcPosRect->top);
    HRGN clipRgn = CreateRectRgnIndirect(&clipRect);
1041
    SetWindowRgn(_inplacewnd, clipRgn, FALSE);
1042

1043
    //RedrawWindow(_videownd, &posRect, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
1044 1045
};

1046 1047 1048 1049 1050
void VLCPlugin::freezeEvents(BOOL freeze)
{
    vlcConnectionPointContainer->freezeEvents(freeze);
};

1051 1052
void VLCPlugin::firePropChangedEvent(DISPID dispid)
{
1053
    vlcConnectionPointContainer->firePropChangedEvent(dispid);
1054 1055
};

1056 1057 1058
/*
 * Async events
 */
1059 1060 1061 1062 1063 1064
void VLCPlugin::fireOnMediaPlayerMediaChangedEvent()
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerMediaChangedEvent, &dispparamsNoArgs);
};

1065
void VLCPlugin::fireOnMediaPlayerNothingSpecialEvent()
1066 1067
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1068
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerNothingSpecialEvent, &dispparamsNoArgs);
1069 1070
};

1071
void VLCPlugin::fireOnMediaPlayerOpeningEvent()
1072 1073
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1074
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerOpeningEvent, &dispparamsNoArgs);
1075 1076
};

1077
void VLCPlugin::fireOnMediaPlayerBufferingEvent(float cache)
1078
{
1079 1080 1081 1082 1083
    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;
1084
    params.rgvarg[0].lVal = static_cast<LONG>(cache);
1085 1086 1087
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerBufferingEvent, &params);
1088 1089
};

1090
void VLCPlugin::fireOnMediaPlayerPlayingEvent()
1091 1092
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1093
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerPlayingEvent, &dispparamsNoArgs);
1094 1095
};

1096
void VLCPlugin::fireOnMediaPlayerPausedEvent()
1097 1098
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1099
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerPausedEvent, &dispparamsNoArgs);
1100 1101
};

1102
void VLCPlugin::fireOnMediaPlayerEncounteredErrorEvent()
1103 1104
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1105
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerEncounteredErrorEvent, &dispparamsNoArgs);
1106 1107
};

1108
void VLCPlugin::fireOnMediaPlayerEndReachedEvent()
1109 1110
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1111
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerEndReachedEvent, &dispparamsNoArgs);
1112 1113
};

1114
void VLCPlugin::fireOnMediaPlayerStoppedEvent()
1115 1116
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1117
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerStoppedEvent, &dispparamsNoArgs);
1118 1119
};

1120 1121 1122 1123 1124 1125 1126
void VLCPlugin::fireOnMediaPlayerStopAsyncDoneEvent()
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerStopAsyncDoneEvent, &dispparamsNoArgs);
};


1127
void VLCPlugin::fireOnMediaPlayerForwardEvent()
1128 1129
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1130
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerForwardEvent, &dispparamsNoArgs);
1131 1132
};

1133
void VLCPlugin::fireOnMediaPlayerBackwardEvent()
1134 1135
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1136
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerBackwardEvent, &dispparamsNoArgs);
1137 1138
};

1139
void VLCPlugin::fireOnMediaPlayerTimeChangedEvent(libvlc_time_t  time)
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;
1146
    params.rgvarg[0].lVal = static_cast<LONG>(time);
1147 1148
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
1149
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerTimeChangedEvent, &params);
1150 1151
};

1152
void VLCPlugin::fireOnMediaPlayerPositionChangedEvent(float position)
1153
{
1154 1155 1156 1157
    DISPPARAMS params;
    params.cArgs = 1;
    params.rgvarg = (VARIANTARG *) CoTaskMemAlloc(sizeof(VARIANTARG) * params.cArgs) ;
    memset(params.rgvarg, 0, sizeof(VARIANTARG) * params.cArgs);
1158 1159
    params.rgvarg[0].vt = VT_R4;
    params.rgvarg[0].fltVal = position;
1160 1161
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
1162
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerPositionChangedEvent, &params);
1163 1164
};

1165
void VLCPlugin::fireOnMediaPlayerSeekableChangedEvent(VARIANT_BOOL seekable)
1166
{
1167 1168 1169 1170 1171 1172 1173 1174
    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;
1175
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerSeekableChangedEvent, &params);
1176 1177
}

1178
void VLCPlugin::fireOnMediaPlayerPausableChangedEvent(VARIANT_BOOL pausable)
1179
{
1180 1181 1182 1183 1184 1185 1186 1187
    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;
1188
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerPausableChangedEvent, &params);
1189 1190
};

1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
void VLCPlugin::fireOnMediaPlayerTitleChangedEvent(int title)
{
    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_I2;
    params.rgvarg[0].iVal = title;
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerTitleChangedEvent, &params);
};

void VLCPlugin::fireOnMediaPlayerLengthChangedEvent(long length)
{
    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 = length;
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerLengthChangedEvent, &params);
}

1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 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
void VLCPlugin::fireClickEvent()
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
    vlcConnectionPointContainer->fireEvent(DISPID_CLICK, &dispparamsNoArgs);
}

void VLCPlugin::fireDblClickEvent()
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
    vlcConnectionPointContainer->fireEvent(DISPID_DBLCLICK, &dispparamsNoArgs);
}

void VLCPlugin::fireMouseDownEvent(short nButton, short nShiftState, int x, int y)
{
    DISPPARAMS params;
    params.cArgs = 4;
    params.rgvarg = (VARIANTARG *) CoTaskMemAlloc(sizeof(VARIANTARG) * params.cArgs);
    memset(params.rgvarg, 0, sizeof(VARIANTARG) * params.cArgs);
    params.rgvarg[3].vt = VT_I2;
    params.rgvarg[3].iVal = nButton;
    params.rgvarg[2].vt = VT_I2;
    params.rgvarg[2].iVal = nShiftState;
    params.rgvarg[1].vt = VT_I4;
    params.rgvarg[1].lVal = x;
    params.rgvarg[0].vt = VT_I4;
    params.rgvarg[0].lVal = y;
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
    vlcConnectionPointContainer->fireEvent(DISPID_MOUSEDOWN, &params);
}

void VLCPlugin::fireMouseMoveEvent(short nButton, short nShiftState, int x, int y)
{
    DISPPARAMS params;
    params.cArgs = 4;
    params.rgvarg = (VARIANTARG *) CoTaskMemAlloc(sizeof(VARIANTARG) * params.cArgs);
    memset(params.rgvarg, 0, sizeof(VARIANTARG) * params.cArgs);
    params.rgvarg[3].vt = VT_I2;
    params.rgvarg[3].iVal = nButton;
    params.rgvarg[2].vt = VT_I2;
    params.rgvarg[2].iVal = nShiftState;
    params.rgvarg[1].vt = VT_I4;
    params.rgvarg[1].lVal = x;
    params.rgvarg[0].vt = VT_I4;
    params.rgvarg[0].lVal = y;
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
    vlcConnectionPointContainer->fireEvent(DISPID_MOUSEMOVE, &params);
}


void VLCPlugin::fireMouseUpEvent(short nButton, short nShiftState, int x, int y)
{
    DISPPARAMS params;
    params.cArgs = 4;
    params.rgvarg = (VARIANTARG *) CoTaskMemAlloc(sizeof(VARIANTARG) * params.cArgs);
    memset(params.rgvarg, 0, sizeof(VARIANTARG) * params.cArgs);
    params.rgvarg[3].vt = VT_I2;
    params.rgvarg[3].iVal = nButton;
    params.rgvarg[2].vt = VT_I2;
    params.rgvarg[2].iVal = nShiftState;
    params.rgvarg[1].vt = VT_I4;
    params.rgvarg[1].lVal = x;
    params.rgvarg[0].vt = VT_I4;
    params.rgvarg[0].lVal = y;
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
    vlcConnectionPointContainer->fireEvent(DISPID_MOUSEUP, &params);
}

void VLCPlugin::fireKeyDownEvent(short nChar, short nShiftState)
{
    DISPPARAMS params;
    params.cArgs = 2;
    params.rgvarg = (VARIANTARG *) CoTaskMemAlloc(sizeof(VARIANTARG) * params.cArgs);
    memset(params.rgvarg, 0, sizeof(VARIANTARG) * params.cArgs);
    //freed by VLCDispatchEvent::~VLCDispatchEvent
    params.rgvarg[1].vt = VT_I2 | VT_BYREF;
    params.rgvarg[1].piVal = new short(nChar);
    params.rgvarg[0].vt = VT_I2;
    params.rgvarg[0].iVal = nShiftState;
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
    vlcConnectionPointContainer->fireEvent(DISPID_KEYDOWN, &params);
}

void VLCPlugin::fireKeyPressEvent(short nChar)
1304 1305 1306
{
    DISPPARAMS params;
    params.cArgs = 1;
1307
    params.rgvarg = (VARIANTARG *) CoTaskMemAlloc(sizeof(VARIANTARG) * params.cArgs);
1308
    memset(params.rgvarg, 0, sizeof(VARIANTARG) * params.cArgs);
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
    //freed by VLCDispatchEvent::~VLCDispatchEvent
    params.rgvarg[0].vt = VT_I2 | VT_BYREF;
    params.rgvarg[0].piVal = new short(nChar);
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
    vlcConnectionPointContainer->fireEvent(DISPID_KEYPRESS, &params);
}

void VLCPlugin::fireKeyUpEvent(short nChar, short nShiftState)
{
    DISPPARAMS params;
    params.cArgs = 2;
    params.rgvarg = (VARIANTARG *) CoTaskMemAlloc(sizeof(VARIANTARG) * params.cArgs);
    memset(params.rgvarg, 0, sizeof(VARIANTARG) * params.cArgs);
    //freed by VLCDispatchEvent::~VLCDispatchEvent
    params.rgvarg[1].vt = VT_I2 | VT_BYREF;
    params.rgvarg[1].piVal = new short(nChar);
1326
    params.rgvarg[0].vt = VT_I2;
1327
    params.rgvarg[0].iVal = nShiftState;
1328 1329
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
1330
    vlcConnectionPointContainer->fireEvent(DISPID_KEYUP, &params);
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
}

void VLCPlugin::fireOnMediaPlayerVoutEvent(int count)
{
    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_I2;
    params.rgvarg[0].iVal = count;
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerVoutEvent, &params);
}

void VLCPlugin::fireOnMediaPlayerMutedEvent()
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerMutedEvent, &dispparamsNoArgs);
}

void VLCPlugin::fireOnMediaPlayerUnmutedEvent()
{
    DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerUnmutedEvent, &dispparamsNoArgs);
}

void VLCPlugin::fireOnMediaPlayerAudioVolumeEvent(float volume)
{
    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_R4;
    params.rgvarg[0].fltVal = volume;
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerAudioVolumeEvent, &params);
}

1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
void VLCPlugin::fireOnMediaPlayerChapterChangedEvent(int chapter)
{
    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_I2;
    params.rgvarg[0].iVal = chapter;
    params.rgdispidNamedArgs = NULL;
    params.cNamedArgs = 0;
1381
    vlcConnectionPointContainer->fireEvent(DISPID_MediaPlayerChapterChangedEvent, &params);
1382 1383
}

1384 1385
/* */

1386
void VLCPlugin::set_player_window()
JP Dinger's avatar
JP Dinger committed
1387
{
1388 1389
    if (_WindowsManager.getHolderWnd())
        m_player.get_mp().setHwnd( _WindowsManager.getHolderWnd()->hWnd() );
JP Dinger's avatar
JP Dinger committed
1390 1391
}

1392
#define B(val) ((val) ? 0xFFFF : 0x0000)
1393

1394 1395
void VLCPlugin::player_register_events()
{
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
    auto& em = m_player.get_mp().eventManager();
    em.onMediaChanged([this](VLC::MediaPtr) {
        fireOnMediaPlayerMediaChangedEvent();
    });
    em.onNothingSpecial([this] {
        fireOnMediaPlayerNothingSpecialEvent();
    });
    em.onOpening([this] {
        fireOnMediaPlayerOpeningEvent();
    });
    em.onBuffering([this](float b) {
        fireOnMediaPlayerBufferingEvent(b);
    });
    em.onPlaying([this] {
        fireOnMediaPlayerPlayingEvent();
    });
    em.onPaused([this] {
        fireOnMediaPlayerPausedEvent();
    });
    em.onStopped([this] {
        fireOnMediaPlayerStoppedEvent();
    });
    em.onForward([this] {
        fireOnMediaPlayerForwardEvent();
    });
    em.onBackward([this] {
        fireOnMediaPlayerBackwardEvent();
    });
    em.onEndReached([this] {
        fireOnMediaPlayerEndReachedEvent();
    });
    em.onEncounteredError([this] {
        fireOnMediaPlayerEncounteredErrorEvent();
    });
    em.onTimeChanged([this] (int64_t time) {
        fireOnMediaPlayerTimeChangedEvent( time );
    });
    em.onPositionChanged([this](float pos) {
        fireOnMediaPlayerPositionChangedEvent( pos );
    });
    em.onSeekableChanged([this](bool b) {
        fireOnMediaPlayerSeekableChangedEvent( B( b ) );
    });
    em.onPausableChanged([this](bool b) {
        fireOnMediaPlayerPausableChangedEvent( B( b ) );
    });
    em.onTitleChanged([this](int t) {
        fireOnMediaPlayerTitleChangedEvent( t );
    });
    em.onLengthChanged( [this]( int64_t length ) {
        fireOnMediaPlayerLengthChangedEvent( length );
    });
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
    em.onChapterChanged( [this]( int chapter ) {
        fireOnMediaPlayerChapterChangedEvent( chapter );
    });
    em.onVout( [this]( int count ) {
        fireOnMediaPlayerVoutEvent( count );
    });
    em.onMuted( [this] {
        fireOnMediaPlayerMutedEvent();
    });
    em.onUnmuted( [this] {
        fireOnMediaPlayerUnmutedEvent();
    });
    em.onAudioVolume( [this]( float volume ) {
        fireOnMediaPlayerAudioVolumeEvent( volume );
    });
1463 1464
}

1465
#undef B