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 HRESULT VLCControl::CreateTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
551 HRESULT hr = E_INVALIDARG;
552 if( VT_ERROR == V_VT(options) )
554 if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
556 // optional parameter not set
562 else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
569 else if( VT_DISPATCH == V_VT(options) )
571 // collection parameter
573 V_VT(&colEnum) = VT_UNKNOWN;
574 hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
577 IEnumVARIANT *enumVar;
578 hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
585 *cOptions = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
586 if( NULL != *cOptions )
588 ZeroMemory(*cOptions, sizeof(char *)*capacity);
589 while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
591 if( VT_BSTR == V_VT(&option) )
593 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
594 (*cOptions)[pos] = cOption;
595 if( NULL != cOption )
598 if( pos == capacity )
600 char **moreOptions = (char **)CoTaskMemRealloc(*cOptions, (capacity+16)*sizeof(char *));
601 if( NULL != moreOptions )
603 ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
605 *cOptions = moreOptions;
612 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
613 E_OUTOFMEMORY : E_INVALIDARG;
618 VariantClear(&option);
623 // free already processed elements
624 FreeTargetOptions(*cOptions, *cOptionCount);
634 else if( V_ISARRAY(options) )
637 SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
639 if( SafeArrayGetDim(array) != 1 )
644 SafeArrayGetLBound(array, 1, &lBound);
645 SafeArrayGetUBound(array, 1, &uBound);
647 // have we got any options
648 if( uBound >= lBound )
651 hr = SafeArrayGetVartype(array, &vType);
657 // marshall options into an array of C strings
658 if( VT_VARIANT == vType )
660 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
661 if( NULL == *cOptions )
662 return E_OUTOFMEMORY;
664 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
665 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
668 hr = SafeArrayGetElement(array, &pos, &option);
671 if( VT_BSTR == V_VT(&option) )
673 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
674 (*cOptions)[pos-lBound] = cOption;
675 if( NULL == cOption )
676 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
677 E_OUTOFMEMORY : E_INVALIDARG;
681 VariantClear(&option);
685 else if( VT_BSTR == vType )
687 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
688 if( NULL == *cOptions )
689 return E_OUTOFMEMORY;
691 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
692 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
695 hr = SafeArrayGetElement(array, &pos, &option);
698 char *cOption = CStrFromBSTR(codePage, option);
700 (*cOptions)[pos-lBound] = cOption;
701 if( NULL == cOption )
702 hr = ( SysStringLen(option) > 0 ) ?
703 E_OUTOFMEMORY : E_INVALIDARG;
704 SysFreeString(option);
714 *cOptionCount = pos-lBound;
717 // free already processed elements
718 FreeTargetOptions(*cOptions, *cOptionCount);
733 ** use VARIANT rather than a SAFEARRAY as argument type
734 ** for compatibility with some scripting language (JScript)
737 STDMETHODIMP VLCControl::addTarget( BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
739 if( 0 == SysStringLen(uri) )
743 HRESULT hr = _p_instance->getVLCObject(&i_vlc);
746 char *cUri = CStrFromBSTR(CP_UTF8, uri);
748 return E_OUTOFMEMORY;
753 if( FAILED(CreateTargetOptions(CP_UTF8, &options, &cOptions, &cOptionsCount)) )
756 if( VLC_SUCCESS <= VLC_AddTarget(i_vlc, cUri, (const char **)cOptions, cOptionsCount, mode, position) )
759 if( mode & PLAYLIST_GO )
760 _p_instance->fireOnPlayEvent();
765 if( mode & PLAYLIST_GO )
766 _p_instance->fireOnStopEvent();
769 FreeTargetOptions(cOptions, cOptionsCount);
775 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
781 HRESULT result = _p_instance->getVLCObject(&i_vlc);
782 if( SUCCEEDED(result) )
784 *index = VLC_PlaylistIndex(i_vlc);
791 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
794 HRESULT result = _p_instance->getVLCObject(&i_vlc);
795 if( SUCCEEDED(result) )
797 *count = VLC_PlaylistNumberOfItems(i_vlc);
804 STDMETHODIMP VLCControl::playlistNext(void)
807 HRESULT result = _p_instance->getVLCObject(&i_vlc);
808 if( SUCCEEDED(result) )
810 VLC_PlaylistNext(i_vlc);
816 STDMETHODIMP VLCControl::playlistPrev(void)
819 HRESULT result = _p_instance->getVLCObject(&i_vlc);
820 if( SUCCEEDED(result) )
822 VLC_PlaylistPrev(i_vlc);
828 STDMETHODIMP VLCControl::playlistClear(void)
831 HRESULT result = _p_instance->getVLCObject(&i_vlc);
832 if( SUCCEEDED(result) )
834 VLC_PlaylistClear(i_vlc);
840 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
842 if( NULL == version )
845 const char *versionStr = VLC_Version();
846 if( NULL != versionStr )
848 *version = BSTRFromCStr(_p_instance->getCodePage(), versionStr);
850 return NULL == *version ? E_OUTOFMEMORY : NOERROR;
856 STDMETHODIMP VLCControl::get_MRL(BSTR *mrl)
861 *mrl = SysAllocStringLen(_p_instance->getMRL(),
862 SysStringLen(_p_instance->getMRL()));
866 STDMETHODIMP VLCControl::put_MRL(BSTR mrl)
868 _p_instance->setMRL(mrl);
873 STDMETHODIMP VLCControl::get_AutoPlay(VARIANT_BOOL *autoplay)
875 if( NULL == autoplay )
878 *autoplay = _p_instance->getAutoPlay() ? VARIANT_TRUE: VARIANT_FALSE;
882 STDMETHODIMP VLCControl::put_AutoPlay(VARIANT_BOOL autoplay)
884 _p_instance->setAutoPlay((VARIANT_FALSE != autoplay) ? TRUE: FALSE);
888 STDMETHODIMP VLCControl::get_AutoLoop(VARIANT_BOOL *autoloop)
890 if( NULL == autoloop )
893 *autoloop = _p_instance->getAutoLoop() ? VARIANT_TRUE: VARIANT_FALSE;
897 STDMETHODIMP VLCControl::put_AutoLoop(VARIANT_BOOL autoloop)
899 _p_instance->setAutoLoop((VARIANT_FALSE != autoloop) ? TRUE: FALSE);