vlccontrol.cpp 22.4 KB
Newer Older
1 2 3
/*****************************************************************************
 * vlccontrol.cpp: ActiveX control for VLC
 *****************************************************************************
4
 * Copyright (C) 2005 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include "plugin.h"
#include "vlccontrol.h"

#include "utils.h"

28 29
using namespace std;

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
VLCControl::~VLCControl()
{
    if( _p_typeinfo )
        _p_typeinfo->Release();
};

HRESULT VLCControl::getTypeInfo(void)
{
    HRESULT hr = NOERROR;
    if( NULL == _p_typeinfo )
    {
        ITypeLib *p_typelib;

        HRESULT hr = _p_instance->getTypeLib(LOCALE_USER_DEFAULT, &p_typelib);
        if( SUCCEEDED(hr) )
        {
            hr = p_typelib->GetTypeInfoOfGuid(IID_IVLCControl, &_p_typeinfo);
            if( FAILED(hr) )
            {
                _p_typeinfo = NULL;
            }
            p_typelib->Release();
        }
    }
    return hr;
};

STDMETHODIMP VLCControl::GetTypeInfoCount(UINT* pctInfo)
{
Damien Fouilleul's avatar
Damien Fouilleul committed
59 60 61
    if( NULL == pctInfo )
        return E_INVALIDARG;

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    if( SUCCEEDED(getTypeInfo()) )
        *pctInfo = 1;
    else
        *pctInfo = 0;

    return NOERROR;
};

STDMETHODIMP VLCControl::GetTypeInfo(UINT iTInfo, LCID lcid, LPTYPEINFO* ppTInfo)
{
    if( NULL == ppTInfo )
        return E_INVALIDARG;

    if( SUCCEEDED(getTypeInfo()) )
    {
        _p_typeinfo->AddRef();
        *ppTInfo = _p_typeinfo;
        return NO_ERROR;
    }
    *ppTInfo = NULL;
    return E_NOTIMPL;
};

STDMETHODIMP VLCControl::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, 
        UINT cNames, LCID lcid, DISPID* rgDispID)
{
    if( SUCCEEDED(getTypeInfo()) )
    {
        return DispGetIDsOfNames(_p_typeinfo, rgszNames, cNames, rgDispID);
    }
    return E_NOTIMPL;
};

STDMETHODIMP VLCControl::Invoke(DISPID dispIdMember, REFIID riid,
        LCID lcid, WORD wFlags, DISPPARAMS* pDispParams,
        VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
    if( SUCCEEDED(getTypeInfo()) )
    {
        return DispInvoke(this, _p_typeinfo, dispIdMember, wFlags, pDispParams,
                pVarResult, pExcepInfo, puArgErr);
    }
    return E_NOTIMPL;
};

STDMETHODIMP VLCControl::get_Visible(VARIANT_BOOL *isVisible)
{
    if( NULL == isVisible )
Damien Fouilleul's avatar
Damien Fouilleul committed
110
        return E_POINTER;
111

112
    *isVisible = _p_instance->getVisible() ? VARIANT_TRUE : VARIANT_FALSE;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

    return NOERROR;
};
        
STDMETHODIMP VLCControl::put_Visible(VARIANT_BOOL isVisible)
{
    _p_instance->setVisible(isVisible != VARIANT_FALSE);

    return NOERROR;
};

STDMETHODIMP VLCControl::play(void)
{
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        VLC_Play(i_vlc);
        _p_instance->fireOnPlayEvent();
        return NOERROR;
    }
    return E_UNEXPECTED;
};
 
STDMETHODIMP VLCControl::pause(void)
{
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        VLC_Pause(i_vlc);
        _p_instance->fireOnPauseEvent();
        return NOERROR;
    }
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::stop(void)
{
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        VLC_Stop(i_vlc);
        _p_instance->fireOnStopEvent();
        return NOERROR;
    }
    return E_UNEXPECTED;
}
        
STDMETHODIMP VLCControl::get_Playing(VARIANT_BOOL *isPlaying)
{
    if( NULL == isPlaying )
Damien Fouilleul's avatar
Damien Fouilleul committed
163
        return E_POINTER;
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        *isPlaying = VLC_IsPlaying(i_vlc) ? VARIANT_TRUE : VARIANT_FALSE;
        return NOERROR;
    }
    *isPlaying = VARIANT_FALSE;
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::put_Playing(VARIANT_BOOL isPlaying)
{
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        if( VARIANT_FALSE == isPlaying )
        {
            if( VLC_IsPlaying(i_vlc) )
                VLC_Stop(i_vlc);
        }
        else
        {
            if( ! VLC_IsPlaying(i_vlc) )
                VLC_Play(i_vlc);
        }
        return NOERROR;
    }
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::get_Position(float *position)
{
    if( NULL == position )
Damien Fouilleul's avatar
Damien Fouilleul committed
198
        return E_POINTER;
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        *position = VLC_PositionGet(i_vlc);
        return NOERROR;
    }
    *position = 0.0f;
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::put_Position(float position)
{
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        VLC_PositionSet(i_vlc, position);
        return NOERROR;
    }
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::get_Time(int *seconds)
{
    if( NULL == seconds )
Damien Fouilleul's avatar
Damien Fouilleul committed
224
        return E_POINTER;
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 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 270 271

    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        *seconds = VLC_TimeGet(i_vlc);
        return NOERROR;
    }
    *seconds = 0;
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::put_Time(int seconds)
{
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        VLC_TimeSet(i_vlc, seconds, VLC_FALSE);
        return NOERROR;
    }
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::shuttle(int seconds)
{
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        VLC_TimeSet(i_vlc, seconds, VLC_TRUE);
        return NOERROR;
    }
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::fullscreen(void)
{
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        VLC_FullScreen(i_vlc);
        return NOERROR;
    }
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::get_Length(int *seconds)
{
    if( NULL == seconds )
Damien Fouilleul's avatar
Damien Fouilleul committed
272
        return E_POINTER;
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        *seconds = VLC_LengthGet(i_vlc);
        return NOERROR;
    }
    *seconds = 0;
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::playFaster(void)
{
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        VLC_SpeedFaster(i_vlc);
        return NOERROR;
    }
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::playSlower(void)
{
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        VLC_SpeedSlower(i_vlc);
        return NOERROR;
    }
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::get_Volume(int *volume)
{
    if( NULL == volume )
Damien Fouilleul's avatar
Damien Fouilleul committed
309
        return E_POINTER;
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        *volume  = VLC_VolumeGet(i_vlc);
        return NOERROR;
    }
    *volume = 0;
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::put_Volume(int volume)
{
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        VLC_VolumeSet(i_vlc, volume);
        return NOERROR;
    }
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::toggleMute(void)
{
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        VLC_VolumeMute(i_vlc);
        return NOERROR;
    }
    return E_UNEXPECTED;
};

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
STDMETHODIMP VLCControl::setVariable( BSTR name, VARIANT value)
{
    if( 0 == SysStringLen(name) )
        return E_INVALIDARG;

    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        int codePage = _p_instance->getCodePage();
        char *psz_varname = CStrFromBSTR(codePage, name);
        if( NULL == psz_varname )
            return E_OUTOFMEMORY;

        HRESULT hr = E_INVALIDARG;
        int i_type;
358 359
        vlc_value_t val;
        
360 361 362 363 364 365 366 367 368 369 370 371 372 373
        if( VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type) )
        {
            VARIANT arg;
            VariantInit(&arg);

            switch( i_type )
            {
                case VLC_VAR_BOOL:
                    hr = VariantChangeType(&value, &arg, 0, VT_BOOL);
                    if( SUCCEEDED(hr) )
                        val.b_bool = (VARIANT_TRUE == V_BOOL(&arg)) ? VLC_TRUE : VLC_FALSE;
                    break;

                case VLC_VAR_INTEGER:
374
                case VLC_VAR_HOTKEY:
375 376 377 378 379 380 381 382 383 384 385 386
                    hr = VariantChangeType(&value, &arg, 0, VT_I4);
                    if( SUCCEEDED(hr) )
                        val.i_int = V_I4(&arg);
                    break;

                case VLC_VAR_FLOAT:
                    hr = VariantChangeType(&value, &arg, 0, VT_R4);
                    if( SUCCEEDED(hr) )
                        val.f_float = V_R4(&arg);
                    break;

                case VLC_VAR_STRING:
387 388 389 390
                case VLC_VAR_MODULE:
                case VLC_VAR_FILE:
                case VLC_VAR_DIRECTORY:
                case VLC_VAR_VARIABLE:
391 392
                    hr = VariantChangeType(&value, &arg, 0, VT_BSTR);
                    if( SUCCEEDED(hr) )
393
                    {
394
                        val.psz_string = CStrFromBSTR(codePage, V_BSTR(&arg));
395 396
                        VariantClear(&arg);
                    }
397 398
                    break;

399 400 401 402 403 404
                case VLC_VAR_TIME:
                    // use a double value to represent time (base is expressed in seconds)
                    hr = VariantChangeType(&value, &arg, 0, VT_R8);
                    if( SUCCEEDED(hr) )
                        val.i_time = (signed __int64)(V_R8(&arg)*1000000.0);
                    break;
405

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
                default:
                    hr = DISP_E_TYPEMISMATCH;
            }
        }
        else {
            // no defined type, defaults to VARIANT type
            hr = NO_ERROR;
            switch( V_VT(&value) )
            {
                case VT_BOOL:
                    val.b_bool = (VARIANT_TRUE == V_BOOL(&value)) ? VLC_TRUE : VLC_FALSE;
                    i_type = VLC_VAR_BOOL;
                    break;
                case VT_I4:
                    val.i_int = V_I4(&value);
                    i_type = VLC_VAR_INTEGER;
                    break;
                case VT_R4:
                    val.f_float = V_R4(&value);
                    i_type = VLC_VAR_FLOAT;
                    break;
                case VT_BSTR:
                    val.psz_string = CStrFromBSTR(codePage, V_BSTR(&value));
                    i_type = VLC_VAR_STRING;
                    break;
                case VT_R8:
                    // use a double value to represent time (base is expressed in seconds)
                    val.i_time = (signed __int64)(V_R8(&value)*1000000.0);
                    i_type = VLC_VAR_TIME;
                    break;
                default:
                    hr = DISP_E_TYPEMISMATCH;
438 439
            }
        }
440 441 442 443 444 445 446
        if( SUCCEEDED(hr) )
        {
            hr = (VLC_SUCCESS == VLC_VariableSet(i_vlc, psz_varname, val)) ? NOERROR : E_FAIL;

            if( (VLC_VAR_STRING == i_type) && (NULL != val.psz_string) )
                free(val.psz_string);
        }
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
        free(psz_varname);

        return hr;
    }
    return E_UNEXPECTED;
};

STDMETHODIMP VLCControl::getVariable( BSTR name, VARIANT *value)
{
    if( 0 == SysStringLen(name) )
        return E_INVALIDARG;

    if( NULL == value )
        return E_POINTER;

    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        int codePage = _p_instance->getCodePage();
        char *psz_varname = CStrFromBSTR(codePage, name);
        if( NULL == psz_varname )
            return E_OUTOFMEMORY;

        HRESULT hr = E_INVALIDARG;

        vlc_value_t val;
        int i_type;

        if( (VLC_SUCCESS == VLC_VariableGet(i_vlc, psz_varname, &val))
         && (VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type)) )
        {
            hr = NOERROR;
            switch( i_type )
            {
                case VLC_VAR_BOOL:
                    V_VT(value) = VT_BOOL;
                    V_BOOL(value) = val.b_bool ? VARIANT_TRUE : VARIANT_FALSE;
                    break;

                case VLC_VAR_INTEGER:
487
                case VLC_VAR_HOTKEY:
488 489 490 491 492 493 494 495 496 497
                    V_VT(value) = VT_I4;
                    V_I4(value) = val.i_int;
                    break;

                case VLC_VAR_FLOAT:
                    V_VT(value) = VT_R4;
                    V_R4(value) = val.f_float;
                    break;

                case VLC_VAR_STRING:
498 499 500 501
                case VLC_VAR_MODULE:
                case VLC_VAR_FILE:
                case VLC_VAR_DIRECTORY:
                case VLC_VAR_VARIABLE:
502 503 504 505
                    V_VT(value) = VT_BSTR;
                    V_BSTR(value) = BSTRFromCStr(codePage, val.psz_string);
                    free(val.psz_string);
                    break;
506 507 508 509 510 511 512

                case VLC_VAR_TIME:
                    // use a double value to represent time (base is expressed in seconds)
                    V_VT(value) = VT_R8;
                    V_R8(value) = ((double)val.i_time)/1000000.0;
                    break;

513 514 515 516 517 518 519 520 521 522
                default:
                    hr = DISP_E_TYPEMISMATCH;
            }
        }
        free(psz_varname);
        return hr;
    }
    return E_UNEXPECTED;
};

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 561 562 563 564 565 566 567 568 569 570 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 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 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
static void freeTargetOptions(char **cOptions, int cOptionCount)
{
    // clean up 
    for( long pos=0; pos<cOptionCount; ++pos )
    {
        char *cOption = cOptions[pos];
        if( NULL != cOption )
            free(cOption);
        else
            break;
    }
    if( NULL != cOptions )
        free(cOptions);
};

static HRESULT createTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
{
    HRESULT hr = E_INVALIDARG;
    if( VT_ERROR == V_VT(options) )
    {
        if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
        {
            // optional parameter not set
            *cOptions = NULL;
            *cOptionCount = 0;
            return NOERROR;
        }
    }
    else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
    {
        // null parameter
        *cOptions = NULL;
        *cOptionCount = 0;
        return NOERROR;
    }
    else if( VT_DISPATCH == V_VT(options) )
    {
        // collection parameter
        VARIANT colEnum;
        V_VT(&colEnum) = VT_UNKNOWN;
        hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
        if( SUCCEEDED(hr) )
        {
            IEnumVARIANT *enumVar;
            hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
            if( SUCCEEDED(hr) )
            {
                long pos = 0;
                long capacity = 16;
                VARIANT option;

                *cOptions = (char **)malloc(capacity*sizeof(char *));
                if( NULL != *cOptions )
                {
                    ZeroMemory(*cOptions, sizeof(char *)*capacity);
                    while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
                    {
                        if( VT_BSTR == V_VT(&option) )
                        {
                            char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
                            (*cOptions)[pos] = cOption;
                            if( NULL != cOption )
                            {
                                ++pos;
                                if( pos == capacity )
                                {
                                    char **moreOptions = (char **)realloc(*cOptions, (capacity+16)*sizeof(char *));
                                    if( NULL != moreOptions )
                                    {
                                        ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
                                        capacity += 16;
                                        *cOptions = moreOptions;
                                    }
                                    else
                                        hr = E_OUTOFMEMORY;
                                }
                            }
                            else
                                hr = E_OUTOFMEMORY;
                        }
                        else
                            hr = E_INVALIDARG;

                        VariantClear(&option);
                    }
                    *cOptionCount = pos;
                    if( FAILED(hr) )
                    {
                        // free already processed elements
                        freeTargetOptions(*cOptions, *cOptionCount);
                    }
                }
                else
                    hr = E_OUTOFMEMORY;
                enumVar->Release();
            }
        }
    }
    else if( V_ISARRAY(options) )
    {
        // array parameter
        SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);

        if( SafeArrayGetDim(array) != 1 )
            return E_INVALIDARG;

        long lBound = 0;
        long uBound = 0;
        SafeArrayGetLBound(array, 1, &lBound);
        SafeArrayGetUBound(array, 1, &uBound);

        // have we got any options
        if( uBound > lBound )
        {
            VARTYPE vType;
            HRESULT hr = SafeArrayGetVartype(array, &vType);
            if( FAILED(hr) )
                return hr;

            long pos;

            // marshall options into an array of C strings
            if( VT_VARIANT == vType )
            {
                *cOptions = (char **)malloc(sizeof(char *)*(uBound-lBound));
                if( NULL != options )
                    return E_OUTOFMEMORY;

                for(pos=lBound; SUCCEEDED(hr) && (pos<uBound); ++pos )
                {
                    VARIANT option;
                    hr = SafeArrayGetElement(array, &pos, &option);
                    if( SUCCEEDED(hr) )
                    {
                        if( VT_BSTR == V_VT(&option) ) 
                        {
                            char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
                            (*cOptions)[pos-lBound] = cOption;
                            if( NULL == cOption )
                                hr = E_OUTOFMEMORY;
                        }
                        else
                            hr = E_INVALIDARG;
                        VariantClear(&option);
                    }
                }
            }
            else if( VT_BSTR == vType )
            {
                *cOptions = (char **)malloc(sizeof(char *)*(uBound-lBound));
                if( NULL != options )
                    return E_OUTOFMEMORY;

                ZeroMemory(cOptions, sizeof(char *)*(uBound-lBound));
                for(pos=lBound; SUCCEEDED(hr) && (pos<uBound); ++pos )
                {
                    BSTR option;
                    hr = SafeArrayGetElement(array, &pos, &option);
                    if( SUCCEEDED(hr) )
                    {
                        char *cOption = CStrFromBSTR(codePage, option);
                        (*cOptions)[pos-lBound] = cOption;
                        if( NULL == cOption )
                            hr = E_OUTOFMEMORY;
                        SysFreeString(option);
                    }
                }
            }
            else
                // unsupported type
                return E_INVALIDARG;

            *cOptionCount = pos-lBound;
            if( FAILED(hr) )
            {
                // free already processed elements
                freeTargetOptions(*cOptions, *cOptionCount);
            }
        }
        else
        {
            // empty array
            *cOptions = NULL;
            *cOptionCount = 0;
            return NOERROR;
        }
    }
    return hr;
};

/*
** use VARIANT rather than a SAFEARRAY as argument type
** for compatibility with some scripting language (JScript)
*/

STDMETHODIMP VLCControl::addTarget( BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
{
Damien Fouilleul's avatar
Damien Fouilleul committed
720
    if( 0 == SysStringLen(uri) )
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
        return E_INVALIDARG;

    HRESULT hr = E_UNEXPECTED;

    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        int codePage = _p_instance->getCodePage();
        char *cUri = CStrFromBSTR(codePage, uri);
        if( NULL == cUri )
            return E_OUTOFMEMORY;

        int cOptionsCount;
        char **cOptions;

        if( FAILED(createTargetOptions(codePage, &options, &cOptions, &cOptionsCount)) )
            return E_INVALIDARG;

739 740 741 742 743 744 745 746 747 748 749 750
        if( VLC_SUCCESS <= VLC_AddTarget(i_vlc, cUri, (const char **)cOptions, cOptionsCount, mode, position) )
        {
            hr = NOERROR;
            if( mode & VLCPlayListGo )
                _p_instance->fireOnPlayEvent();
        }
        else
        {
            hr = E_FAIL;
            if( mode & VLCPlayListGo )
                _p_instance->fireOnStopEvent();
        }
751 752 753 754 755 756 757 758 759 760

        freeTargetOptions(cOptions, cOptionsCount);
        free(cUri);
    }
    return hr;
};
        
STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
{
    if( NULL == index )
Damien Fouilleul's avatar
Damien Fouilleul committed
761
        return E_POINTER;
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819

    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        *index = VLC_PlaylistIndex(i_vlc);
        return NOERROR;
    }
    *index = 0;
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
{
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        *count = VLC_PlaylistNumberOfItems(i_vlc);
        return NOERROR;
    }
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::playlistNext(void)
{
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        VLC_PlaylistNext(i_vlc);
        return NOERROR;
    }
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::playlistPrev(void)
{
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        VLC_PlaylistPrev(i_vlc);
        return NOERROR;
    }
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::playlistClear(void)
{
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
        VLC_PlaylistClear(i_vlc);
        return NOERROR;
    }
    return E_UNEXPECTED;
};
        
STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
{
    if( NULL == version )
Damien Fouilleul's avatar
Damien Fouilleul committed
820
        return E_POINTER;
821 822 823 824 825 826 827 828 829 830 831 832

    const char *versionStr = VLC_Version();
    if( NULL != versionStr )
    {
        *version = BSTRFromCStr(_p_instance->getCodePage(), versionStr);
        
        return NULL == *version ? E_OUTOFMEMORY : NOERROR;
    }
    *version = NULL;
    return E_FAIL;
};