vlccontrol.cpp 23.6 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)
{
damienf's avatar
damienf 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 )
damienf's avatar
damienf 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

    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;
158
};
159 160 161 162
        
STDMETHODIMP VLCControl::get_Playing(VARIANT_BOOL *isPlaying)
{
    if( NULL == isPlaying )
damienf's avatar
damienf committed
163
        return E_POINTER;
164 165 166 167 168 169 170 171 172 173 174 175 176 177

    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::get_Position(float *position)
{
    if( NULL == position )
damienf's avatar
damienf committed
178
        return E_POINTER;
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

    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 )
damienf's avatar
damienf committed
204
        return E_POINTER;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 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

    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 )
damienf's avatar
damienf committed
252
        return E_POINTER;
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

    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 )
damienf's avatar
damienf committed
289
        return E_POINTER;
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

    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;
};

323
STDMETHODIMP VLCControl::setVariable(BSTR name, VARIANT value)
324 325 326 327 328 329 330 331 332 333 334 335 336 337
{
    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;
338 339
        vlc_value_t val;
        
340 341 342 343 344 345 346 347
        if( VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type) )
        {
            VARIANT arg;
            VariantInit(&arg);

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

                case VLC_VAR_INTEGER:
354
                case VLC_VAR_HOTKEY:
355
                    hr = VariantChangeType(&arg, &value, 0, VT_I4);
356 357 358 359 360
                    if( SUCCEEDED(hr) )
                        val.i_int = V_I4(&arg);
                    break;

                case VLC_VAR_FLOAT:
361
                    hr = VariantChangeType(&arg, &value, 0, VT_R4);
362 363 364 365 366
                    if( SUCCEEDED(hr) )
                        val.f_float = V_R4(&arg);
                    break;

                case VLC_VAR_STRING:
367 368 369 370
                case VLC_VAR_MODULE:
                case VLC_VAR_FILE:
                case VLC_VAR_DIRECTORY:
                case VLC_VAR_VARIABLE:
371
                    hr = VariantChangeType(&arg, &value, 0, VT_BSTR);
372
                    if( SUCCEEDED(hr) )
373
                    {
374
                        val.psz_string = CStrFromBSTR(codePage, V_BSTR(&arg));
375 376
                        VariantClear(&arg);
                    }
377 378
                    break;

379 380
                case VLC_VAR_TIME:
                    // use a double value to represent time (base is expressed in seconds)
381
                    hr = VariantChangeType(&arg, &value, 0, VT_R8);
382 383 384
                    if( SUCCEEDED(hr) )
                        val.i_time = (signed __int64)(V_R8(&arg)*1000000.0);
                    break;
385

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
                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;
418 419
            }
        }
420 421 422 423 424
        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) )
damienf's avatar
 
damienf committed
425
                CoTaskMemFree(val.psz_string);
426
        }
damienf's avatar
 
damienf committed
427
        CoTaskMemFree(psz_varname);
428 429 430 431 432 433 434 435 436 437 438

        return hr;
    }
    return E_UNEXPECTED;
};

STDMETHODIMP VLCControl::getVariable( BSTR name, VARIANT *value)
{
    if( NULL == value )
        return E_POINTER;

439 440 441 442 443
    VariantInit(value);

    if( 0 == SysStringLen(name) )
        return E_INVALIDARG;

444 445 446
    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
447
        UINT codePage = _p_instance->getCodePage();
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
        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:
469
                case VLC_VAR_HOTKEY:
470 471 472 473 474 475 476 477 478 479
                    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:
480 481 482 483
                case VLC_VAR_MODULE:
                case VLC_VAR_FILE:
                case VLC_VAR_DIRECTORY:
                case VLC_VAR_VARIABLE:
484 485
                    V_VT(value) = VT_BSTR;
                    V_BSTR(value) = BSTRFromCStr(codePage, val.psz_string);
486 487
                    if( NULL != val.psz_string)
                        free(val.psz_string);
488
                    break;
489 490 491 492 493 494 495

                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;

496 497 498 499
                default:
                    hr = DISP_E_TYPEMISMATCH;
            }
        }
damienf's avatar
 
damienf committed
500
        CoTaskMemFree(psz_varname);
501 502 503 504 505
        return hr;
    }
    return E_UNEXPECTED;
};

506 507 508
static void freeTargetOptions(char **cOptions, int cOptionCount)
{
    // clean up 
damienf's avatar
 
damienf committed
509
    if( NULL != cOptions )
510
    {
damienf's avatar
 
damienf committed
511 512 513 514 515 516 517 518 519
        for( int pos=0; pos<cOptionCount; ++pos )
        {
            char *cOption = cOptions[pos];
            if( NULL != cOption )
                CoTaskMemFree(cOption);
            else
                break;
        }
        CoTaskMemFree(cOptions);
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
    }
};

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;

damienf's avatar
 
damienf committed
559
                *cOptions = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
560 561 562 563 564 565 566 567 568 569 570 571 572 573
                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 )
                                {
damienf's avatar
 
damienf committed
574
                                    char **moreOptions = (char **)CoTaskMemRealloc(*cOptions, (capacity+16)*sizeof(char *));
575 576 577 578 579 580 581 582 583 584 585
                                    if( NULL != moreOptions )
                                    {
                                        ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
                                        capacity += 16;
                                        *cOptions = moreOptions;
                                    }
                                    else
                                        hr = E_OUTOFMEMORY;
                                }
                            }
                            else
damienf's avatar
 
damienf committed
586 587
                                hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
                                    E_OUTOFMEMORY : E_INVALIDARG;
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
                        }
                        else
                            hr = E_INVALIDARG;

                        VariantClear(&option);
                    }
                    *cOptionCount = pos;
                    if( FAILED(hr) )
                    {
                        // free already processed elements
                        freeTargetOptions(*cOptions, *cOptionCount);
                    }
                }
                else
                    hr = E_OUTOFMEMORY;
damienf's avatar
 
damienf committed
603

604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
                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
damienf's avatar
damienf committed
622
        if( uBound >= lBound )
623 624
        {
            VARTYPE vType;
damienf's avatar
 
damienf committed
625
            hr = SafeArrayGetVartype(array, &vType);
626 627 628 629 630 631 632 633
            if( FAILED(hr) )
                return hr;

            long pos;

            // marshall options into an array of C strings
            if( VT_VARIANT == vType )
            {
damienf's avatar
 
damienf committed
634 635
                *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound));
                if( NULL == *cOptions )
636 637
                    return E_OUTOFMEMORY;

damienf's avatar
 
damienf committed
638
                ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound));
damienf's avatar
damienf committed
639
                for(pos=lBound; SUCCEEDED(hr) && (pos<=uBound); ++pos )
640 641 642 643 644 645 646 647 648 649
                {
                    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 )
damienf's avatar
 
damienf committed
650 651
                                hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
                                    E_OUTOFMEMORY : E_INVALIDARG;
652 653 654 655 656 657 658 659 660
                        }
                        else
                            hr = E_INVALIDARG;
                        VariantClear(&option);
                    }
                }
            }
            else if( VT_BSTR == vType )
            {
damienf's avatar
 
damienf committed
661 662
                *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound));
                if( NULL == *cOptions )
663 664
                    return E_OUTOFMEMORY;

damienf's avatar
 
damienf committed
665 666
                ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound));
                for(pos=lBound; (pos<uBound) && SUCCEEDED(hr); ++pos )
667 668 669 670 671 672
                {
                    BSTR option;
                    hr = SafeArrayGetElement(array, &pos, &option);
                    if( SUCCEEDED(hr) )
                    {
                        char *cOption = CStrFromBSTR(codePage, option);
damienf's avatar
 
damienf committed
673

674 675
                        (*cOptions)[pos-lBound] = cOption;
                        if( NULL == cOption )
damienf's avatar
 
damienf committed
676 677
                            hr = ( SysStringLen(option) > 0 ) ?
                                E_OUTOFMEMORY : E_INVALIDARG;
678 679 680 681
                        SysFreeString(option);
                    }
                }
            }
damienf's avatar
 
damienf committed
682 683
            else 
            {
684 685
                // unsupported type
                return E_INVALIDARG;
damienf's avatar
 
damienf committed
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

            *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)
{
damienf's avatar
damienf committed
713
    if( 0 == SysStringLen(uri) )
714 715 716 717 718 719 720
        return E_INVALIDARG;

    HRESULT hr = E_UNEXPECTED;

    int i_vlc = _p_instance->getVLCObject();
    if( i_vlc )
    {
721
        char *cUri = CStrFromBSTR(CP_UTF8, uri);
722 723 724 725 726 727
        if( NULL == cUri )
            return E_OUTOFMEMORY;

        int cOptionsCount;
        char **cOptions;

728
        if( FAILED(createTargetOptions(CP_UTF8, &options, &cOptions, &cOptionsCount)) )
729 730
            return E_INVALIDARG;

731 732 733
        if( VLC_SUCCESS <= VLC_AddTarget(i_vlc, cUri, (const char **)cOptions, cOptionsCount, mode, position) )
        {
            hr = NOERROR;
734
            if( mode & PLAYLIST_GO )
735 736 737 738 739
                _p_instance->fireOnPlayEvent();
        }
        else
        {
            hr = E_FAIL;
740
            if( mode & PLAYLIST_GO )
741 742
                _p_instance->fireOnStopEvent();
        }
743 744

        freeTargetOptions(cOptions, cOptionsCount);
damienf's avatar
 
damienf committed
745
        CoTaskMemFree(cUri);
746 747 748 749 750 751 752
    }
    return hr;
};
        
STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
{
    if( NULL == index )
damienf's avatar
damienf committed
753
        return E_POINTER;
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772

    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;
    }
damienf's avatar
 
damienf committed
773
    *count = 0;
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
    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 )
damienf's avatar
damienf committed
813
        return E_POINTER;
814 815 816 817 818 819 820 821 822 823 824 825

    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;
};
 
826 827 828 829 830
STDMETHODIMP VLCControl::get_MRL(BSTR *mrl)
{
    if( NULL == mrl )
        return E_POINTER;

damienf's avatar
damienf committed
831 832
    *mrl = SysAllocStringLen(_p_instance->getMRL(),
                SysStringLen(_p_instance->getMRL()));
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
    return NOERROR;
};

STDMETHODIMP VLCControl::put_MRL(BSTR mrl)
{
    _p_instance->setMRL(mrl);

    return S_OK;
};

STDMETHODIMP VLCControl::get_AutoPlay(VARIANT_BOOL *autoplay)
{
    if( NULL == autoplay )
        return E_POINTER;

    *autoplay = _p_instance->getAutoPlay() ? VARIANT_TRUE: VARIANT_FALSE;
    return S_OK;
};

STDMETHODIMP VLCControl::put_AutoPlay(VARIANT_BOOL autoplay)
{
    _p_instance->setAutoPlay((VARIANT_FALSE != autoplay) ? TRUE: FALSE);
    return S_OK;
};

STDMETHODIMP VLCControl::get_AutoLoop(VARIANT_BOOL *autoloop)
{
    if( NULL == autoloop )
        return E_POINTER;

    *autoloop = _p_instance->getAutoLoop() ? VARIANT_TRUE: VARIANT_FALSE;
    return S_OK;
};

STDMETHODIMP VLCControl::put_AutoLoop(VARIANT_BOOL autoloop)
{
    _p_instance->setAutoLoop((VARIANT_FALSE != autoloop) ? TRUE: FALSE);
    return S_OK;
};