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 *volume = _p_instance->getVolume();
295 STDMETHODIMP VLCControl::put_Volume(int volume)
297 _p_instance->setVolume(volume);
301 STDMETHODIMP VLCControl::toggleMute(void)
303 int i_vlc = _p_instance->getVLCObject();
306 VLC_VolumeMute(i_vlc);
312 STDMETHODIMP VLCControl::setVariable(BSTR name, VARIANT value)
314 if( 0 == SysStringLen(name) )
317 int i_vlc = _p_instance->getVLCObject();
320 int codePage = _p_instance->getCodePage();
321 char *psz_varname = CStrFromBSTR(codePage, name);
322 if( NULL == psz_varname )
323 return E_OUTOFMEMORY;
325 HRESULT hr = E_INVALIDARG;
329 if( VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type) )
337 hr = VariantChangeType(&arg, &value, 0, VT_BOOL);
339 val.b_bool = (VARIANT_TRUE == V_BOOL(&arg)) ? VLC_TRUE : VLC_FALSE;
342 case VLC_VAR_INTEGER:
344 hr = VariantChangeType(&arg, &value, 0, VT_I4);
346 val.i_int = V_I4(&arg);
350 hr = VariantChangeType(&arg, &value, 0, VT_R4);
352 val.f_float = V_R4(&arg);
358 case VLC_VAR_DIRECTORY:
359 case VLC_VAR_VARIABLE:
360 hr = VariantChangeType(&arg, &value, 0, VT_BSTR);
363 i_type = VLC_VAR_STRING;
364 val.psz_string = CStrFromBSTR(codePage, V_BSTR(&arg));
370 // use a double value to represent time (base is expressed in seconds)
371 hr = VariantChangeType(&arg, &value, 0, VT_R8);
373 val.i_time = (signed __int64)(V_R8(&arg)*1000000.0);
377 hr = DISP_E_TYPEMISMATCH;
381 // no defined type, defaults to VARIANT type
383 switch( V_VT(&value) )
386 val.b_bool = (VARIANT_TRUE == V_BOOL(&value)) ? VLC_TRUE : VLC_FALSE;
387 i_type = VLC_VAR_BOOL;
390 val.i_int = V_I4(&value);
391 i_type = VLC_VAR_INTEGER;
394 val.f_float = V_R4(&value);
395 i_type = VLC_VAR_FLOAT;
398 val.psz_string = CStrFromBSTR(codePage, V_BSTR(&value));
399 i_type = VLC_VAR_STRING;
402 // use a double value to represent time (base is expressed in seconds)
403 val.i_time = (signed __int64)(V_R8(&value)*1000000.0);
404 i_type = VLC_VAR_TIME;
407 hr = DISP_E_TYPEMISMATCH;
412 hr = (VLC_SUCCESS == VLC_VariableSet(i_vlc, psz_varname, val)) ? NOERROR : E_FAIL;
414 if( (VLC_VAR_STRING == i_type) && (NULL != val.psz_string) )
415 CoTaskMemFree(val.psz_string);
417 CoTaskMemFree(psz_varname);
424 STDMETHODIMP VLCControl::getVariable( BSTR name, VARIANT *value)
431 if( 0 == SysStringLen(name) )
434 int i_vlc = _p_instance->getVLCObject();
437 UINT codePage = _p_instance->getCodePage();
438 char *psz_varname = CStrFromBSTR(codePage, name);
439 if( NULL == psz_varname )
440 return E_OUTOFMEMORY;
442 HRESULT hr = E_INVALIDARG;
447 if( (VLC_SUCCESS == VLC_VariableGet(i_vlc, psz_varname, &val))
448 && (VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type)) )
454 V_VT(value) = VT_BOOL;
455 V_BOOL(value) = val.b_bool ? VARIANT_TRUE : VARIANT_FALSE;
458 case VLC_VAR_INTEGER:
461 V_I4(value) = val.i_int;
466 V_R4(value) = val.f_float;
472 case VLC_VAR_DIRECTORY:
473 case VLC_VAR_VARIABLE:
474 V_VT(value) = VT_BSTR;
475 V_BSTR(value) = BSTRFromCStr(codePage, val.psz_string);
476 if( NULL != val.psz_string)
477 free(val.psz_string);
481 // use a double value to represent time (base is expressed in seconds)
483 V_R8(value) = ((double)val.i_time)/1000000.0;
487 hr = DISP_E_TYPEMISMATCH;
490 CoTaskMemFree(psz_varname);
496 static void freeTargetOptions(char **cOptions, int cOptionCount)
499 if( NULL != cOptions )
501 for( int pos=0; pos<cOptionCount; ++pos )
503 char *cOption = cOptions[pos];
504 if( NULL != cOption )
505 CoTaskMemFree(cOption);
509 CoTaskMemFree(cOptions);
513 static HRESULT createTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
515 HRESULT hr = E_INVALIDARG;
516 if( VT_ERROR == V_VT(options) )
518 if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
520 // optional parameter not set
526 else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
533 else if( VT_DISPATCH == V_VT(options) )
535 // collection parameter
537 V_VT(&colEnum) = VT_UNKNOWN;
538 hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
541 IEnumVARIANT *enumVar;
542 hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
549 *cOptions = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
550 if( NULL != *cOptions )
552 ZeroMemory(*cOptions, sizeof(char *)*capacity);
553 while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
555 if( VT_BSTR == V_VT(&option) )
557 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
558 (*cOptions)[pos] = cOption;
559 if( NULL != cOption )
562 if( pos == capacity )
564 char **moreOptions = (char **)CoTaskMemRealloc(*cOptions, (capacity+16)*sizeof(char *));
565 if( NULL != moreOptions )
567 ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
569 *cOptions = moreOptions;
576 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
577 E_OUTOFMEMORY : E_INVALIDARG;
582 VariantClear(&option);
587 // free already processed elements
588 freeTargetOptions(*cOptions, *cOptionCount);
598 else if( V_ISARRAY(options) )
601 SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
603 if( SafeArrayGetDim(array) != 1 )
608 SafeArrayGetLBound(array, 1, &lBound);
609 SafeArrayGetUBound(array, 1, &uBound);
611 // have we got any options
612 if( uBound >= lBound )
615 hr = SafeArrayGetVartype(array, &vType);
621 // marshall options into an array of C strings
622 if( VT_VARIANT == vType )
624 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
625 if( NULL == *cOptions )
626 return E_OUTOFMEMORY;
628 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
629 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
632 hr = SafeArrayGetElement(array, &pos, &option);
635 if( VT_BSTR == V_VT(&option) )
637 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
638 (*cOptions)[pos-lBound] = cOption;
639 if( NULL == cOption )
640 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
641 E_OUTOFMEMORY : E_INVALIDARG;
645 VariantClear(&option);
649 else if( VT_BSTR == vType )
651 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
652 if( NULL == *cOptions )
653 return E_OUTOFMEMORY;
655 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
656 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
659 hr = SafeArrayGetElement(array, &pos, &option);
662 char *cOption = CStrFromBSTR(codePage, option);
664 (*cOptions)[pos-lBound] = cOption;
665 if( NULL == cOption )
666 hr = ( SysStringLen(option) > 0 ) ?
667 E_OUTOFMEMORY : E_INVALIDARG;
668 SysFreeString(option);
678 *cOptionCount = pos-lBound;
681 // free already processed elements
682 freeTargetOptions(*cOptions, *cOptionCount);
697 ** use VARIANT rather than a SAFEARRAY as argument type
698 ** for compatibility with some scripting language (JScript)
701 STDMETHODIMP VLCControl::addTarget( BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
703 if( 0 == SysStringLen(uri) )
706 HRESULT hr = E_UNEXPECTED;
708 int i_vlc = _p_instance->getVLCObject();
711 char *cUri = CStrFromBSTR(CP_UTF8, uri);
713 return E_OUTOFMEMORY;
718 if( FAILED(createTargetOptions(CP_UTF8, &options, &cOptions, &cOptionsCount)) )
721 if( VLC_SUCCESS <= VLC_AddTarget(i_vlc, cUri, (const char **)cOptions, cOptionsCount, mode, position) )
724 if( mode & PLAYLIST_GO )
725 _p_instance->fireOnPlayEvent();
730 if( mode & PLAYLIST_GO )
731 _p_instance->fireOnStopEvent();
734 freeTargetOptions(cOptions, cOptionsCount);
740 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
745 int i_vlc = _p_instance->getVLCObject();
748 *index = VLC_PlaylistIndex(i_vlc);
755 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
757 int i_vlc = _p_instance->getVLCObject();
760 *count = VLC_PlaylistNumberOfItems(i_vlc);
767 STDMETHODIMP VLCControl::playlistNext(void)
769 int i_vlc = _p_instance->getVLCObject();
772 VLC_PlaylistNext(i_vlc);
778 STDMETHODIMP VLCControl::playlistPrev(void)
780 int i_vlc = _p_instance->getVLCObject();
783 VLC_PlaylistPrev(i_vlc);
789 STDMETHODIMP VLCControl::playlistClear(void)
791 int i_vlc = _p_instance->getVLCObject();
794 VLC_PlaylistClear(i_vlc);
800 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
802 if( NULL == version )
805 const char *versionStr = VLC_Version();
806 if( NULL != versionStr )
808 *version = BSTRFromCStr(_p_instance->getCodePage(), versionStr);
810 return NULL == *version ? E_OUTOFMEMORY : NOERROR;
816 STDMETHODIMP VLCControl::get_MRL(BSTR *mrl)
821 *mrl = SysAllocStringLen(_p_instance->getMRL(),
822 SysStringLen(_p_instance->getMRL()));
826 STDMETHODIMP VLCControl::put_MRL(BSTR mrl)
828 _p_instance->setMRL(mrl);
833 STDMETHODIMP VLCControl::get_AutoPlay(VARIANT_BOOL *autoplay)
835 if( NULL == autoplay )
838 *autoplay = _p_instance->getAutoPlay() ? VARIANT_TRUE: VARIANT_FALSE;
842 STDMETHODIMP VLCControl::put_AutoPlay(VARIANT_BOOL autoplay)
844 _p_instance->setAutoPlay((VARIANT_FALSE != autoplay) ? TRUE: FALSE);
848 STDMETHODIMP VLCControl::get_AutoLoop(VARIANT_BOOL *autoloop)
850 if( NULL == autoloop )
853 *autoloop = _p_instance->getAutoLoop() ? VARIANT_TRUE: VARIANT_FALSE;
857 STDMETHODIMP VLCControl::put_AutoLoop(VARIANT_BOOL autoloop)
859 _p_instance->setAutoLoop((VARIANT_FALSE != autoloop) ? TRUE: FALSE);