1 /*****************************************************************************
2 * vlccontrol.cpp: ActiveX control for VLC
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN
6 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
24 #include "vlccontrol.h"
30 VLCControl::~VLCControl()
33 _p_typeinfo->Release();
36 HRESULT VLCControl::getTypeInfo(void)
39 if( NULL == _p_typeinfo )
43 HRESULT hr = _p_instance->getTypeLib(LOCALE_USER_DEFAULT, &p_typelib);
46 hr = p_typelib->GetTypeInfoOfGuid(IID_IVLCControl, &_p_typeinfo);
57 STDMETHODIMP VLCControl::GetTypeInfoCount(UINT* pctInfo)
62 if( SUCCEEDED(getTypeInfo()) )
70 STDMETHODIMP VLCControl::GetTypeInfo(UINT iTInfo, LCID lcid, LPTYPEINFO* ppTInfo)
75 if( SUCCEEDED(getTypeInfo()) )
77 _p_typeinfo->AddRef();
78 *ppTInfo = _p_typeinfo;
85 STDMETHODIMP VLCControl::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames,
86 UINT cNames, LCID lcid, DISPID* rgDispID)
88 if( SUCCEEDED(getTypeInfo()) )
90 return DispGetIDsOfNames(_p_typeinfo, rgszNames, cNames, rgDispID);
95 STDMETHODIMP VLCControl::Invoke(DISPID dispIdMember, REFIID riid,
96 LCID lcid, WORD wFlags, DISPPARAMS* pDispParams,
97 VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
99 if( SUCCEEDED(getTypeInfo()) )
101 return DispInvoke(this, _p_typeinfo, dispIdMember, wFlags, pDispParams,
102 pVarResult, pExcepInfo, puArgErr);
107 STDMETHODIMP VLCControl::get_Visible(VARIANT_BOOL *isVisible)
109 if( NULL == isVisible )
112 *isVisible = _p_instance->getVisible() ? VARIANT_TRUE : VARIANT_FALSE;
117 STDMETHODIMP VLCControl::put_Visible(VARIANT_BOOL isVisible)
119 _p_instance->setVisible(isVisible != VARIANT_FALSE);
124 STDMETHODIMP VLCControl::play(void)
126 int i_vlc = _p_instance->getVLCObject();
130 _p_instance->fireOnPlayEvent();
136 STDMETHODIMP VLCControl::pause(void)
138 int i_vlc = _p_instance->getVLCObject();
142 _p_instance->fireOnPauseEvent();
148 STDMETHODIMP VLCControl::stop(void)
150 int i_vlc = _p_instance->getVLCObject();
154 _p_instance->fireOnStopEvent();
160 STDMETHODIMP VLCControl::get_Playing(VARIANT_BOOL *isPlaying)
162 if( NULL == isPlaying )
165 int i_vlc = _p_instance->getVLCObject();
168 *isPlaying = VLC_IsPlaying(i_vlc) ? VARIANT_TRUE : VARIANT_FALSE;
171 *isPlaying = VARIANT_FALSE;
175 STDMETHODIMP VLCControl::put_Playing(VARIANT_BOOL isPlaying)
177 int i_vlc = _p_instance->getVLCObject();
180 if( VARIANT_FALSE == isPlaying )
182 if( VLC_IsPlaying(i_vlc) )
187 if( ! VLC_IsPlaying(i_vlc) )
195 STDMETHODIMP VLCControl::get_Position(float *position)
197 if( NULL == position )
200 int i_vlc = _p_instance->getVLCObject();
203 *position = VLC_PositionGet(i_vlc);
210 STDMETHODIMP VLCControl::put_Position(float position)
212 int i_vlc = _p_instance->getVLCObject();
215 VLC_PositionSet(i_vlc, position);
221 STDMETHODIMP VLCControl::get_Time(int *seconds)
223 if( NULL == seconds )
226 int i_vlc = _p_instance->getVLCObject();
229 *seconds = VLC_TimeGet(i_vlc);
236 STDMETHODIMP VLCControl::put_Time(int seconds)
238 int i_vlc = _p_instance->getVLCObject();
241 VLC_TimeSet(i_vlc, seconds, VLC_FALSE);
247 STDMETHODIMP VLCControl::shuttle(int seconds)
249 int i_vlc = _p_instance->getVLCObject();
252 VLC_TimeSet(i_vlc, seconds, VLC_TRUE);
258 STDMETHODIMP VLCControl::fullscreen(void)
260 int i_vlc = _p_instance->getVLCObject();
263 VLC_FullScreen(i_vlc);
269 STDMETHODIMP VLCControl::get_Length(int *seconds)
271 if( NULL == seconds )
274 int i_vlc = _p_instance->getVLCObject();
277 *seconds = VLC_LengthGet(i_vlc);
284 STDMETHODIMP VLCControl::playFaster(void)
286 int i_vlc = _p_instance->getVLCObject();
289 VLC_SpeedFaster(i_vlc);
295 STDMETHODIMP VLCControl::playSlower(void)
297 int i_vlc = _p_instance->getVLCObject();
300 VLC_SpeedSlower(i_vlc);
306 STDMETHODIMP VLCControl::get_Volume(int *volume)
311 int i_vlc = _p_instance->getVLCObject();
314 *volume = VLC_VolumeGet(i_vlc);
321 STDMETHODIMP VLCControl::put_Volume(int volume)
323 int i_vlc = _p_instance->getVLCObject();
326 VLC_VolumeSet(i_vlc, volume);
332 STDMETHODIMP VLCControl::toggleMute(void)
334 int i_vlc = _p_instance->getVLCObject();
337 VLC_VolumeMute(i_vlc);
343 STDMETHODIMP VLCControl::setVariable( BSTR name, VARIANT value)
345 if( 0 == SysStringLen(name) )
348 int i_vlc = _p_instance->getVLCObject();
351 int codePage = _p_instance->getCodePage();
352 char *psz_varname = CStrFromBSTR(codePage, name);
353 if( NULL == psz_varname )
354 return E_OUTOFMEMORY;
356 HRESULT hr = E_INVALIDARG;
360 if( VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type) )
368 hr = VariantChangeType(&value, &arg, 0, VT_BOOL);
370 val.b_bool = (VARIANT_TRUE == V_BOOL(&arg)) ? VLC_TRUE : VLC_FALSE;
373 case VLC_VAR_INTEGER:
375 hr = VariantChangeType(&value, &arg, 0, VT_I4);
377 val.i_int = V_I4(&arg);
381 hr = VariantChangeType(&value, &arg, 0, VT_R4);
383 val.f_float = V_R4(&arg);
389 case VLC_VAR_DIRECTORY:
390 case VLC_VAR_VARIABLE:
391 hr = VariantChangeType(&value, &arg, 0, VT_BSTR);
394 val.psz_string = CStrFromBSTR(codePage, V_BSTR(&arg));
400 // use a double value to represent time (base is expressed in seconds)
401 hr = VariantChangeType(&value, &arg, 0, VT_R8);
403 val.i_time = (signed __int64)(V_R8(&arg)*1000000.0);
407 hr = DISP_E_TYPEMISMATCH;
411 // no defined type, defaults to VARIANT type
413 switch( V_VT(&value) )
416 val.b_bool = (VARIANT_TRUE == V_BOOL(&value)) ? VLC_TRUE : VLC_FALSE;
417 i_type = VLC_VAR_BOOL;
420 val.i_int = V_I4(&value);
421 i_type = VLC_VAR_INTEGER;
424 val.f_float = V_R4(&value);
425 i_type = VLC_VAR_FLOAT;
428 val.psz_string = CStrFromBSTR(codePage, V_BSTR(&value));
429 i_type = VLC_VAR_STRING;
432 // use a double value to represent time (base is expressed in seconds)
433 val.i_time = (signed __int64)(V_R8(&value)*1000000.0);
434 i_type = VLC_VAR_TIME;
437 hr = DISP_E_TYPEMISMATCH;
442 hr = (VLC_SUCCESS == VLC_VariableSet(i_vlc, psz_varname, val)) ? NOERROR : E_FAIL;
444 if( (VLC_VAR_STRING == i_type) && (NULL != val.psz_string) )
445 free(val.psz_string);
454 STDMETHODIMP VLCControl::getVariable( BSTR name, VARIANT *value)
456 if( 0 == SysStringLen(name) )
462 int i_vlc = _p_instance->getVLCObject();
465 int codePage = _p_instance->getCodePage();
466 char *psz_varname = CStrFromBSTR(codePage, name);
467 if( NULL == psz_varname )
468 return E_OUTOFMEMORY;
470 HRESULT hr = E_INVALIDARG;
475 if( (VLC_SUCCESS == VLC_VariableGet(i_vlc, psz_varname, &val))
476 && (VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type)) )
482 V_VT(value) = VT_BOOL;
483 V_BOOL(value) = val.b_bool ? VARIANT_TRUE : VARIANT_FALSE;
486 case VLC_VAR_INTEGER:
489 V_I4(value) = val.i_int;
494 V_R4(value) = val.f_float;
500 case VLC_VAR_DIRECTORY:
501 case VLC_VAR_VARIABLE:
502 V_VT(value) = VT_BSTR;
503 V_BSTR(value) = BSTRFromCStr(codePage, val.psz_string);
504 free(val.psz_string);
508 // use a double value to represent time (base is expressed in seconds)
510 V_R8(value) = ((double)val.i_time)/1000000.0;
514 hr = DISP_E_TYPEMISMATCH;
523 static void freeTargetOptions(char **cOptions, int cOptionCount)
526 for( long pos=0; pos<cOptionCount; ++pos )
528 char *cOption = cOptions[pos];
529 if( NULL != cOption )
534 if( NULL != cOptions )
538 static HRESULT createTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
540 HRESULT hr = E_INVALIDARG;
541 if( VT_ERROR == V_VT(options) )
543 if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
545 // optional parameter not set
551 else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
558 else if( VT_DISPATCH == V_VT(options) )
560 // collection parameter
562 V_VT(&colEnum) = VT_UNKNOWN;
563 hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
566 IEnumVARIANT *enumVar;
567 hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
574 *cOptions = (char **)malloc(capacity*sizeof(char *));
575 if( NULL != *cOptions )
577 ZeroMemory(*cOptions, sizeof(char *)*capacity);
578 while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
580 if( VT_BSTR == V_VT(&option) )
582 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
583 (*cOptions)[pos] = cOption;
584 if( NULL != cOption )
587 if( pos == capacity )
589 char **moreOptions = (char **)realloc(*cOptions, (capacity+16)*sizeof(char *));
590 if( NULL != moreOptions )
592 ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
594 *cOptions = moreOptions;
606 VariantClear(&option);
611 // free already processed elements
612 freeTargetOptions(*cOptions, *cOptionCount);
621 else if( V_ISARRAY(options) )
624 SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
626 if( SafeArrayGetDim(array) != 1 )
631 SafeArrayGetLBound(array, 1, &lBound);
632 SafeArrayGetUBound(array, 1, &uBound);
634 // have we got any options
635 if( uBound > lBound )
638 HRESULT hr = SafeArrayGetVartype(array, &vType);
644 // marshall options into an array of C strings
645 if( VT_VARIANT == vType )
647 *cOptions = (char **)malloc(sizeof(char *)*(uBound-lBound));
648 if( NULL != options )
649 return E_OUTOFMEMORY;
651 for(pos=lBound; SUCCEEDED(hr) && (pos<uBound); ++pos )
654 hr = SafeArrayGetElement(array, &pos, &option);
657 if( VT_BSTR == V_VT(&option) )
659 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
660 (*cOptions)[pos-lBound] = cOption;
661 if( NULL == cOption )
666 VariantClear(&option);
670 else if( VT_BSTR == vType )
672 *cOptions = (char **)malloc(sizeof(char *)*(uBound-lBound));
673 if( NULL != options )
674 return E_OUTOFMEMORY;
676 ZeroMemory(cOptions, sizeof(char *)*(uBound-lBound));
677 for(pos=lBound; SUCCEEDED(hr) && (pos<uBound); ++pos )
680 hr = SafeArrayGetElement(array, &pos, &option);
683 char *cOption = CStrFromBSTR(codePage, option);
684 (*cOptions)[pos-lBound] = cOption;
685 if( NULL == cOption )
687 SysFreeString(option);
695 *cOptionCount = pos-lBound;
698 // free already processed elements
699 freeTargetOptions(*cOptions, *cOptionCount);
714 ** use VARIANT rather than a SAFEARRAY as argument type
715 ** for compatibility with some scripting language (JScript)
718 STDMETHODIMP VLCControl::addTarget( BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
720 if( 0 == SysStringLen(uri) )
723 HRESULT hr = E_UNEXPECTED;
725 int i_vlc = _p_instance->getVLCObject();
728 int codePage = _p_instance->getCodePage();
729 char *cUri = CStrFromBSTR(codePage, uri);
731 return E_OUTOFMEMORY;
736 if( FAILED(createTargetOptions(codePage, &options, &cOptions, &cOptionsCount)) )
739 if( VLC_SUCCESS <= VLC_AddTarget(i_vlc, cUri, (const char **)cOptions, cOptionsCount, mode, position) )
742 if( mode & VLCPlayListGo )
743 _p_instance->fireOnPlayEvent();
748 if( mode & VLCPlayListGo )
749 _p_instance->fireOnStopEvent();
752 freeTargetOptions(cOptions, cOptionsCount);
758 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
763 int i_vlc = _p_instance->getVLCObject();
766 *index = VLC_PlaylistIndex(i_vlc);
773 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
775 int i_vlc = _p_instance->getVLCObject();
778 *count = VLC_PlaylistNumberOfItems(i_vlc);
784 STDMETHODIMP VLCControl::playlistNext(void)
786 int i_vlc = _p_instance->getVLCObject();
789 VLC_PlaylistNext(i_vlc);
795 STDMETHODIMP VLCControl::playlistPrev(void)
797 int i_vlc = _p_instance->getVLCObject();
800 VLC_PlaylistPrev(i_vlc);
806 STDMETHODIMP VLCControl::playlistClear(void)
808 int i_vlc = _p_instance->getVLCObject();
811 VLC_PlaylistClear(i_vlc);
817 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
819 if( NULL == version )
822 const char *versionStr = VLC_Version();
823 if( NULL != versionStr )
825 *version = BSTRFromCStr(_p_instance->getCodePage(), versionStr);
827 return NULL == *version ? E_OUTOFMEMORY : NOERROR;