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 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::get_Position(float *position)
177 if( NULL == position )
180 int i_vlc = _p_instance->getVLCObject();
183 *position = VLC_PositionGet(i_vlc);
190 STDMETHODIMP VLCControl::put_Position(float position)
192 int i_vlc = _p_instance->getVLCObject();
195 VLC_PositionSet(i_vlc, position);
201 STDMETHODIMP VLCControl::get_Time(int *seconds)
203 if( NULL == seconds )
206 int i_vlc = _p_instance->getVLCObject();
209 *seconds = VLC_TimeGet(i_vlc);
216 STDMETHODIMP VLCControl::put_Time(int seconds)
218 int i_vlc = _p_instance->getVLCObject();
221 VLC_TimeSet(i_vlc, seconds, VLC_FALSE);
227 STDMETHODIMP VLCControl::shuttle(int seconds)
229 int i_vlc = _p_instance->getVLCObject();
232 VLC_TimeSet(i_vlc, seconds, VLC_TRUE);
238 STDMETHODIMP VLCControl::fullscreen(void)
240 int i_vlc = _p_instance->getVLCObject();
243 VLC_FullScreen(i_vlc);
249 STDMETHODIMP VLCControl::get_Length(int *seconds)
251 if( NULL == seconds )
254 int i_vlc = _p_instance->getVLCObject();
257 *seconds = VLC_LengthGet(i_vlc);
264 STDMETHODIMP VLCControl::playFaster(void)
266 int i_vlc = _p_instance->getVLCObject();
269 VLC_SpeedFaster(i_vlc);
275 STDMETHODIMP VLCControl::playSlower(void)
277 int i_vlc = _p_instance->getVLCObject();
280 VLC_SpeedSlower(i_vlc);
286 STDMETHODIMP VLCControl::get_Volume(int *volume)
291 int i_vlc = _p_instance->getVLCObject();
294 *volume = VLC_VolumeGet(i_vlc);
301 STDMETHODIMP VLCControl::put_Volume(int volume)
303 int i_vlc = _p_instance->getVLCObject();
306 VLC_VolumeSet(i_vlc, volume);
312 STDMETHODIMP VLCControl::toggleMute(void)
314 int i_vlc = _p_instance->getVLCObject();
317 VLC_VolumeMute(i_vlc);
323 STDMETHODIMP VLCControl::setVariable(BSTR name, VARIANT value)
325 if( 0 == SysStringLen(name) )
328 int i_vlc = _p_instance->getVLCObject();
331 int codePage = _p_instance->getCodePage();
332 char *psz_varname = CStrFromBSTR(codePage, name);
333 if( NULL == psz_varname )
334 return E_OUTOFMEMORY;
336 HRESULT hr = E_INVALIDARG;
340 if( VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type) )
348 hr = VariantChangeType(&arg, &value, 0, VT_BOOL);
350 val.b_bool = (VARIANT_TRUE == V_BOOL(&arg)) ? VLC_TRUE : VLC_FALSE;
353 case VLC_VAR_INTEGER:
355 hr = VariantChangeType(&arg, &value, 0, VT_I4);
357 val.i_int = V_I4(&arg);
361 hr = VariantChangeType(&arg, &value, 0, VT_R4);
363 val.f_float = V_R4(&arg);
369 case VLC_VAR_DIRECTORY:
370 case VLC_VAR_VARIABLE:
371 hr = VariantChangeType(&arg, &value, 0, VT_BSTR);
374 i_type = VLC_VAR_STRING;
375 val.psz_string = CStrFromBSTR(codePage, V_BSTR(&arg));
381 // use a double value to represent time (base is expressed in seconds)
382 hr = VariantChangeType(&arg, &value, 0, VT_R8);
384 val.i_time = (signed __int64)(V_R8(&arg)*1000000.0);
388 hr = DISP_E_TYPEMISMATCH;
392 // no defined type, defaults to VARIANT type
394 switch( V_VT(&value) )
397 val.b_bool = (VARIANT_TRUE == V_BOOL(&value)) ? VLC_TRUE : VLC_FALSE;
398 i_type = VLC_VAR_BOOL;
401 val.i_int = V_I4(&value);
402 i_type = VLC_VAR_INTEGER;
405 val.f_float = V_R4(&value);
406 i_type = VLC_VAR_FLOAT;
409 val.psz_string = CStrFromBSTR(codePage, V_BSTR(&value));
410 i_type = VLC_VAR_STRING;
413 // use a double value to represent time (base is expressed in seconds)
414 val.i_time = (signed __int64)(V_R8(&value)*1000000.0);
415 i_type = VLC_VAR_TIME;
418 hr = DISP_E_TYPEMISMATCH;
423 hr = (VLC_SUCCESS == VLC_VariableSet(i_vlc, psz_varname, val)) ? NOERROR : E_FAIL;
425 if( (VLC_VAR_STRING == i_type) && (NULL != val.psz_string) )
426 CoTaskMemFree(val.psz_string);
428 CoTaskMemFree(psz_varname);
435 STDMETHODIMP VLCControl::getVariable( BSTR name, VARIANT *value)
442 if( 0 == SysStringLen(name) )
445 int i_vlc = _p_instance->getVLCObject();
448 UINT codePage = _p_instance->getCodePage();
449 char *psz_varname = CStrFromBSTR(codePage, name);
450 if( NULL == psz_varname )
451 return E_OUTOFMEMORY;
453 HRESULT hr = E_INVALIDARG;
458 if( (VLC_SUCCESS == VLC_VariableGet(i_vlc, psz_varname, &val))
459 && (VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type)) )
465 V_VT(value) = VT_BOOL;
466 V_BOOL(value) = val.b_bool ? VARIANT_TRUE : VARIANT_FALSE;
469 case VLC_VAR_INTEGER:
472 V_I4(value) = val.i_int;
477 V_R4(value) = val.f_float;
483 case VLC_VAR_DIRECTORY:
484 case VLC_VAR_VARIABLE:
485 V_VT(value) = VT_BSTR;
486 V_BSTR(value) = BSTRFromCStr(codePage, val.psz_string);
487 if( NULL != val.psz_string)
488 free(val.psz_string);
492 // use a double value to represent time (base is expressed in seconds)
494 V_R8(value) = ((double)val.i_time)/1000000.0;
498 hr = DISP_E_TYPEMISMATCH;
501 CoTaskMemFree(psz_varname);
507 static void freeTargetOptions(char **cOptions, int cOptionCount)
510 if( NULL != cOptions )
512 for( int pos=0; pos<cOptionCount; ++pos )
514 char *cOption = cOptions[pos];
515 if( NULL != cOption )
516 CoTaskMemFree(cOption);
520 CoTaskMemFree(cOptions);
524 static HRESULT createTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
526 HRESULT hr = E_INVALIDARG;
527 if( VT_ERROR == V_VT(options) )
529 if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
531 // optional parameter not set
537 else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
544 else if( VT_DISPATCH == V_VT(options) )
546 // collection parameter
548 V_VT(&colEnum) = VT_UNKNOWN;
549 hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
552 IEnumVARIANT *enumVar;
553 hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
560 *cOptions = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
561 if( NULL != *cOptions )
563 ZeroMemory(*cOptions, sizeof(char *)*capacity);
564 while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
566 if( VT_BSTR == V_VT(&option) )
568 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
569 (*cOptions)[pos] = cOption;
570 if( NULL != cOption )
573 if( pos == capacity )
575 char **moreOptions = (char **)CoTaskMemRealloc(*cOptions, (capacity+16)*sizeof(char *));
576 if( NULL != moreOptions )
578 ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
580 *cOptions = moreOptions;
587 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
588 E_OUTOFMEMORY : E_INVALIDARG;
593 VariantClear(&option);
598 // free already processed elements
599 freeTargetOptions(*cOptions, *cOptionCount);
609 else if( V_ISARRAY(options) )
612 SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
614 if( SafeArrayGetDim(array) != 1 )
619 SafeArrayGetLBound(array, 1, &lBound);
620 SafeArrayGetUBound(array, 1, &uBound);
622 // have we got any options
623 if( uBound >= lBound )
626 hr = SafeArrayGetVartype(array, &vType);
632 // marshall options into an array of C strings
633 if( VT_VARIANT == vType )
635 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
636 if( NULL == *cOptions )
637 return E_OUTOFMEMORY;
639 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
640 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
643 hr = SafeArrayGetElement(array, &pos, &option);
646 if( VT_BSTR == V_VT(&option) )
648 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
649 (*cOptions)[pos-lBound] = cOption;
650 if( NULL == cOption )
651 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
652 E_OUTOFMEMORY : E_INVALIDARG;
656 VariantClear(&option);
660 else if( VT_BSTR == vType )
662 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
663 if( NULL == *cOptions )
664 return E_OUTOFMEMORY;
666 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
667 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
670 hr = SafeArrayGetElement(array, &pos, &option);
673 char *cOption = CStrFromBSTR(codePage, option);
675 (*cOptions)[pos-lBound] = cOption;
676 if( NULL == cOption )
677 hr = ( SysStringLen(option) > 0 ) ?
678 E_OUTOFMEMORY : E_INVALIDARG;
679 SysFreeString(option);
689 *cOptionCount = pos-lBound;
692 // free already processed elements
693 freeTargetOptions(*cOptions, *cOptionCount);
708 ** use VARIANT rather than a SAFEARRAY as argument type
709 ** for compatibility with some scripting language (JScript)
712 STDMETHODIMP VLCControl::addTarget( BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
714 if( 0 == SysStringLen(uri) )
717 HRESULT hr = E_UNEXPECTED;
719 int i_vlc = _p_instance->getVLCObject();
722 char *cUri = CStrFromBSTR(CP_UTF8, uri);
724 return E_OUTOFMEMORY;
729 if( FAILED(createTargetOptions(CP_UTF8, &options, &cOptions, &cOptionsCount)) )
732 if( VLC_SUCCESS <= VLC_AddTarget(i_vlc, cUri, (const char **)cOptions, cOptionsCount, mode, position) )
735 if( mode & PLAYLIST_GO )
736 _p_instance->fireOnPlayEvent();
741 if( mode & PLAYLIST_GO )
742 _p_instance->fireOnStopEvent();
745 freeTargetOptions(cOptions, cOptionsCount);
751 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
756 int i_vlc = _p_instance->getVLCObject();
759 *index = VLC_PlaylistIndex(i_vlc);
766 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
768 int i_vlc = _p_instance->getVLCObject();
771 *count = VLC_PlaylistNumberOfItems(i_vlc);
778 STDMETHODIMP VLCControl::playlistNext(void)
780 int i_vlc = _p_instance->getVLCObject();
783 VLC_PlaylistNext(i_vlc);
789 STDMETHODIMP VLCControl::playlistPrev(void)
791 int i_vlc = _p_instance->getVLCObject();
794 VLC_PlaylistPrev(i_vlc);
800 STDMETHODIMP VLCControl::playlistClear(void)
802 int i_vlc = _p_instance->getVLCObject();
805 VLC_PlaylistClear(i_vlc);
811 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
813 if( NULL == version )
816 const char *versionStr = VLC_Version();
817 if( NULL != versionStr )
819 *version = BSTRFromCStr(_p_instance->getCodePage(), versionStr);
821 return NULL == *version ? E_OUTOFMEMORY : NOERROR;
827 STDMETHODIMP VLCControl::get_MRL(BSTR *mrl)
832 *mrl = SysAllocStringLen(_p_instance->getMRL(),
833 SysStringLen(_p_instance->getMRL()));
837 STDMETHODIMP VLCControl::put_MRL(BSTR mrl)
839 _p_instance->setMRL(mrl);
844 STDMETHODIMP VLCControl::get_AutoPlay(VARIANT_BOOL *autoplay)
846 if( NULL == autoplay )
849 *autoplay = _p_instance->getAutoPlay() ? VARIANT_TRUE: VARIANT_FALSE;
853 STDMETHODIMP VLCControl::put_AutoPlay(VARIANT_BOOL autoplay)
855 _p_instance->setAutoPlay((VARIANT_FALSE != autoplay) ? TRUE: FALSE);
859 STDMETHODIMP VLCControl::get_AutoLoop(VARIANT_BOOL *autoloop)
861 if( NULL == autoloop )
864 *autoloop = _p_instance->getAutoLoop() ? VARIANT_TRUE: VARIANT_FALSE;
868 STDMETHODIMP VLCControl::put_AutoLoop(VARIANT_BOOL autoloop)
870 _p_instance->setAutoLoop((VARIANT_FALSE != autoloop) ? TRUE: FALSE);