1 /*****************************************************************************
2 * vlccontrol.cpp: ActiveX control for VLC
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
24 #include "vlccontrol.h"
30 VLCControl::~VLCControl()
33 _p_typeinfo->Release();
36 HRESULT VLCControl::getTypeInfo(void)
39 if( NULL == _p_typeinfo )
43 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)
127 HRESULT result = _p_instance->getVLCObject(&i_vlc);
128 if( SUCCEEDED(result) )
131 _p_instance->fireOnPlayEvent();
136 STDMETHODIMP VLCControl::pause(void)
139 HRESULT result = _p_instance->getVLCObject(&i_vlc);
140 if( SUCCEEDED(result) )
143 _p_instance->fireOnPauseEvent();
148 STDMETHODIMP VLCControl::stop(void)
151 HRESULT result = _p_instance->getVLCObject(&i_vlc);
152 if( SUCCEEDED(result) )
155 _p_instance->fireOnStopEvent();
160 STDMETHODIMP VLCControl::get_Playing(VARIANT_BOOL *isPlaying)
162 if( NULL == isPlaying )
165 HRESULT result = NOERROR;
166 if( _p_instance->isRunning() )
169 result = _p_instance->getVLCObject(&i_vlc);
170 if( SUCCEEDED(result) )
172 *isPlaying = VLC_IsPlaying(i_vlc) ? VARIANT_TRUE : VARIANT_FALSE;
176 *isPlaying = VARIANT_FALSE;
180 STDMETHODIMP VLCControl::get_Position(float *position)
182 if( NULL == position )
185 HRESULT result = E_UNEXPECTED;
186 if( _p_instance->isRunning() )
189 result = _p_instance->getVLCObject(&i_vlc);
190 if( SUCCEEDED(result) )
192 *position = VLC_PositionGet(i_vlc);
200 STDMETHODIMP VLCControl::put_Position(float position)
202 HRESULT result = E_UNEXPECTED;
203 if( _p_instance->isRunning() )
206 result = _p_instance->getVLCObject(&i_vlc);
207 if( SUCCEEDED(result) )
209 VLC_PositionSet(i_vlc, position);
215 STDMETHODIMP VLCControl::get_Time(int *seconds)
217 if( NULL == seconds )
220 HRESULT result = NOERROR;
221 if( _p_instance->isRunning() )
224 result = _p_instance->getVLCObject(&i_vlc);
225 if( SUCCEEDED(result) )
227 *seconds = VLC_TimeGet(i_vlc);
231 *seconds = _p_instance->getTime();
236 STDMETHODIMP VLCControl::put_Time(int seconds)
238 _p_instance->setTime(seconds);
243 STDMETHODIMP VLCControl::shuttle(int seconds)
245 HRESULT result = E_UNEXPECTED;
246 if( _p_instance->isRunning() )
249 result = _p_instance->getVLCObject(&i_vlc);
250 if( SUCCEEDED(result) )
252 VLC_TimeSet(i_vlc, seconds, VLC_TRUE);
258 STDMETHODIMP VLCControl::fullscreen(void)
260 HRESULT result = E_UNEXPECTED;
261 if( _p_instance->isRunning() )
264 result = _p_instance->getVLCObject(&i_vlc);
265 if( SUCCEEDED(result) )
267 VLC_FullScreen(i_vlc);
273 STDMETHODIMP VLCControl::get_Length(int *seconds)
275 if( NULL == seconds )
278 HRESULT result = NOERROR;
279 if( _p_instance->isRunning() )
282 result = _p_instance->getVLCObject(&i_vlc);
283 if( SUCCEEDED(result) )
285 *seconds = VLC_LengthGet(i_vlc);
293 STDMETHODIMP VLCControl::playFaster(void)
295 HRESULT result = E_UNEXPECTED;
296 if( _p_instance->isRunning() )
299 result = _p_instance->getVLCObject(&i_vlc);
300 if( SUCCEEDED(result) )
302 VLC_SpeedFaster(i_vlc);
308 STDMETHODIMP VLCControl::playSlower(void)
310 HRESULT result = E_UNEXPECTED;
311 if( _p_instance->isRunning() )
314 result = _p_instance->getVLCObject(&i_vlc);
315 if( SUCCEEDED(result) )
317 VLC_SpeedSlower(i_vlc);
323 STDMETHODIMP VLCControl::get_Volume(int *volume)
328 *volume = _p_instance->getVolume();
332 STDMETHODIMP VLCControl::put_Volume(int volume)
334 _p_instance->setVolume(volume);
338 STDMETHODIMP VLCControl::toggleMute(void)
341 HRESULT result = _p_instance->getVLCObject(&i_vlc);
342 if( SUCCEEDED(result) )
344 VLC_VolumeMute(i_vlc);
349 STDMETHODIMP VLCControl::setVariable(BSTR name, VARIANT value)
351 if( 0 == SysStringLen(name) )
355 HRESULT hr = _p_instance->getVLCObject(&i_vlc);
358 int codePage = _p_instance->getCodePage();
359 char *psz_varname = CStrFromBSTR(codePage, name);
360 if( NULL == psz_varname )
361 return E_OUTOFMEMORY;
366 if( VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type) )
374 hr = VariantChangeType(&arg, &value, 0, VT_BOOL);
376 val.b_bool = (VARIANT_TRUE == V_BOOL(&arg)) ? VLC_TRUE : VLC_FALSE;
379 case VLC_VAR_INTEGER:
381 hr = VariantChangeType(&arg, &value, 0, VT_I4);
383 val.i_int = V_I4(&arg);
387 hr = VariantChangeType(&arg, &value, 0, VT_R4);
389 val.f_float = V_R4(&arg);
395 case VLC_VAR_DIRECTORY:
396 case VLC_VAR_VARIABLE:
397 hr = VariantChangeType(&arg, &value, 0, VT_BSTR);
400 i_type = VLC_VAR_STRING;
401 val.psz_string = CStrFromBSTR(codePage, V_BSTR(&arg));
407 // use a double value to represent time (base is expressed in seconds)
408 hr = VariantChangeType(&arg, &value, 0, VT_R8);
410 val.i_time = (signed __int64)(V_R8(&arg)*1000000.0);
414 hr = DISP_E_TYPEMISMATCH;
418 // no defined type, use type in VARIANT
420 switch( V_VT(&value) )
423 val.b_bool = (VARIANT_TRUE == V_BOOL(&value)) ? VLC_TRUE : VLC_FALSE;
424 i_type = VLC_VAR_BOOL;
427 val.i_int = V_I4(&value);
428 i_type = VLC_VAR_INTEGER;
431 val.f_float = V_R4(&value);
432 i_type = VLC_VAR_FLOAT;
435 val.psz_string = CStrFromBSTR(codePage, V_BSTR(&value));
436 i_type = VLC_VAR_STRING;
439 // use a double value to represent time (base is expressed in seconds)
440 val.i_time = (signed __int64)(V_R8(&value)*1000000.0);
441 i_type = VLC_VAR_TIME;
444 hr = DISP_E_TYPEMISMATCH;
449 hr = (VLC_SUCCESS == VLC_VariableSet(i_vlc, psz_varname, val)) ? NOERROR : E_FAIL;
451 if( (VLC_VAR_STRING == i_type) && (NULL != val.psz_string) )
452 CoTaskMemFree(val.psz_string);
454 CoTaskMemFree(psz_varname);
459 STDMETHODIMP VLCControl::getVariable( BSTR name, VARIANT *value)
466 if( 0 == SysStringLen(name) )
470 HRESULT hr = _p_instance->getVLCObject(&i_vlc);
473 UINT codePage = _p_instance->getCodePage();
474 char *psz_varname = CStrFromBSTR(codePage, name);
475 if( NULL == psz_varname )
476 return E_OUTOFMEMORY;
483 if( (VLC_SUCCESS == VLC_VariableGet(i_vlc, psz_varname, &val))
484 && (VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type)) )
490 V_VT(value) = VT_BOOL;
491 V_BOOL(value) = val.b_bool ? VARIANT_TRUE : VARIANT_FALSE;
494 case VLC_VAR_INTEGER:
497 V_I4(value) = val.i_int;
502 V_R4(value) = val.f_float;
508 case VLC_VAR_DIRECTORY:
509 case VLC_VAR_VARIABLE:
510 V_VT(value) = VT_BSTR;
511 V_BSTR(value) = BSTRFromCStr(codePage, val.psz_string);
512 if( NULL != val.psz_string)
513 free(val.psz_string);
517 // use a double value to represent time (base is expressed in seconds)
519 V_R8(value) = ((double)val.i_time)/1000000.0;
523 hr = DISP_E_TYPEMISMATCH;
526 CoTaskMemFree(psz_varname);
532 void VLCControl::FreeTargetOptions(char **cOptions, int cOptionCount)
535 if( NULL != cOptions )
537 for( int pos=0; pos<cOptionCount; ++pos )
539 char *cOption = cOptions[pos];
540 if( NULL != cOption )
541 CoTaskMemFree(cOption);
545 CoTaskMemFree(cOptions);
549 static HRESULT parseStringOptions(int codePage, BSTR bstr, char*** cOptions, int *cOptionCount)
551 HRESULT hr = E_INVALIDARG;
552 if( SysStringLen(bstr) > 0 )
555 char *s = CStrFromBSTR(codePage, bstr);
560 char **options = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
565 char *end = val + strlen(val);
568 // skip leading blanks
570 && ((*val == ' ' ) || (*val == '\t')) )
574 // skip till we get a blank character
580 if( ('\'' == c) || ('"' == c) )
582 // skip till end of string
583 while( (val < end) && (*(val++) != c ) );
589 if( nOptions == capacity )
592 char **moreOptions = (char **)CoTaskMemRealloc(options, capacity*sizeof(char*));
595 /* failed to allocate more memory */
597 /* return what we got so far */
598 *cOptionCount = nOptions;
602 options = moreOptions;
605 options[nOptions] = (char *)CoTaskMemAlloc(val-start);
606 if( options[nOptions] )
608 memcpy(options[nOptions], start, val-start);
613 /* failed to allocate memory */
615 /* return what we got so far */
616 *cOptionCount = nOptions;
622 // must be end of string
625 *cOptionCount = nOptions;
635 HRESULT VLCControl::CreateTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
637 HRESULT hr = E_INVALIDARG;
638 if( VT_ERROR == V_VT(options) )
640 if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
642 // optional parameter not set
648 else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
655 else if( VT_DISPATCH == V_VT(options) )
657 // if object is a collection, retrieve enumerator
659 V_VT(&colEnum) = VT_UNKNOWN;
660 hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
663 IEnumVARIANT *enumVar;
664 hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
671 *cOptions = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
672 if( NULL != *cOptions )
674 ZeroMemory(*cOptions, sizeof(char *)*capacity);
675 while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
677 if( VT_BSTR == V_VT(&option) )
679 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
680 (*cOptions)[pos] = cOption;
681 if( NULL != cOption )
684 if( pos == capacity )
686 char **moreOptions = (char **)CoTaskMemRealloc(*cOptions, (capacity+16)*sizeof(char *));
687 if( NULL != moreOptions )
689 ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
691 *cOptions = moreOptions;
698 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
699 E_OUTOFMEMORY : E_INVALIDARG;
704 VariantClear(&option);
709 // free already processed elements
710 FreeTargetOptions(*cOptions, *cOptionCount);
721 // coerce object into a string and parse it
723 VariantInit(&v_name);
724 hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
727 hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
728 VariantClear(&v_name);
732 else if( V_ISARRAY(options) )
735 SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
737 if( SafeArrayGetDim(array) != 1 )
742 SafeArrayGetLBound(array, 1, &lBound);
743 SafeArrayGetUBound(array, 1, &uBound);
745 // have we got any options
746 if( uBound >= lBound )
749 hr = SafeArrayGetVartype(array, &vType);
755 // marshall options into an array of C strings
756 if( VT_VARIANT == vType )
758 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
759 if( NULL == *cOptions )
760 return E_OUTOFMEMORY;
762 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
763 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
766 hr = SafeArrayGetElement(array, &pos, &option);
769 if( VT_BSTR == V_VT(&option) )
771 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
772 (*cOptions)[pos-lBound] = cOption;
773 if( NULL == cOption )
774 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
775 E_OUTOFMEMORY : E_INVALIDARG;
779 VariantClear(&option);
783 else if( VT_BSTR == vType )
785 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
786 if( NULL == *cOptions )
787 return E_OUTOFMEMORY;
789 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
790 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
793 hr = SafeArrayGetElement(array, &pos, &option);
796 char *cOption = CStrFromBSTR(codePage, option);
798 (*cOptions)[pos-lBound] = cOption;
799 if( NULL == cOption )
800 hr = ( SysStringLen(option) > 0 ) ?
801 E_OUTOFMEMORY : E_INVALIDARG;
802 SysFreeString(option);
812 *cOptionCount = pos-lBound;
815 // free already processed elements
816 FreeTargetOptions(*cOptions, *cOptionCount);
827 else if( VT_UNKNOWN == V_VT(options) )
829 // coerce object into a string and parse it
831 VariantInit(&v_name);
832 hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
835 hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
836 VariantClear(&v_name);
839 else if( VT_BSTR == V_VT(options) )
841 hr = parseStringOptions(codePage, V_BSTR(options), cOptions, cOptionCount);
847 ** use VARIANT rather than a SAFEARRAY as argument type
848 ** for compatibility with some scripting language (JScript)
851 STDMETHODIMP VLCControl::addTarget( BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
853 if( 0 == SysStringLen(uri) )
857 HRESULT hr = _p_instance->getVLCObject(&i_vlc);
860 char *cUri = CStrFromBSTR(CP_UTF8, uri);
862 return E_OUTOFMEMORY;
867 if( FAILED(CreateTargetOptions(CP_UTF8, &options, &cOptions, &cOptionsCount)) )
870 if( VLC_SUCCESS <= VLC_AddTarget(i_vlc, cUri, (const char **)cOptions, cOptionsCount, mode, position) )
873 if( mode & PLAYLIST_GO )
874 _p_instance->fireOnPlayEvent();
879 if( mode & PLAYLIST_GO )
880 _p_instance->fireOnStopEvent();
883 FreeTargetOptions(cOptions, cOptionsCount);
889 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
895 HRESULT result = _p_instance->getVLCObject(&i_vlc);
896 if( SUCCEEDED(result) )
898 *index = VLC_PlaylistIndex(i_vlc);
905 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
908 HRESULT result = _p_instance->getVLCObject(&i_vlc);
909 if( SUCCEEDED(result) )
911 *count = VLC_PlaylistNumberOfItems(i_vlc);
918 STDMETHODIMP VLCControl::playlistNext(void)
921 HRESULT result = _p_instance->getVLCObject(&i_vlc);
922 if( SUCCEEDED(result) )
924 VLC_PlaylistNext(i_vlc);
930 STDMETHODIMP VLCControl::playlistPrev(void)
933 HRESULT result = _p_instance->getVLCObject(&i_vlc);
934 if( SUCCEEDED(result) )
936 VLC_PlaylistPrev(i_vlc);
942 STDMETHODIMP VLCControl::playlistClear(void)
945 HRESULT result = _p_instance->getVLCObject(&i_vlc);
946 if( SUCCEEDED(result) )
948 VLC_PlaylistClear(i_vlc);
954 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
956 if( NULL == version )
959 const char *versionStr = VLC_Version();
960 if( NULL != versionStr )
962 *version = BSTRFromCStr(CP_UTF8, versionStr);
964 return NULL == *version ? E_OUTOFMEMORY : NOERROR;
970 STDMETHODIMP VLCControl::get_MRL(BSTR *mrl)
975 *mrl = SysAllocStringLen(_p_instance->getMRL(),
976 SysStringLen(_p_instance->getMRL()));
980 STDMETHODIMP VLCControl::put_MRL(BSTR mrl)
982 _p_instance->setMRL(mrl);
987 STDMETHODIMP VLCControl::get_AutoPlay(VARIANT_BOOL *autoplay)
989 if( NULL == autoplay )
992 *autoplay = _p_instance->getAutoPlay() ? VARIANT_TRUE: VARIANT_FALSE;
996 STDMETHODIMP VLCControl::put_AutoPlay(VARIANT_BOOL autoplay)
998 _p_instance->setAutoPlay((VARIANT_FALSE != autoplay) ? TRUE: FALSE);
1002 STDMETHODIMP VLCControl::get_AutoLoop(VARIANT_BOOL *autoloop)
1004 if( NULL == autoloop )
1007 *autoloop = _p_instance->getAutoLoop() ? VARIANT_TRUE: VARIANT_FALSE;
1011 STDMETHODIMP VLCControl::put_AutoLoop(VARIANT_BOOL autoloop)
1013 _p_instance->setAutoLoop((VARIANT_FALSE != autoloop) ? TRUE: FALSE);