1 /*****************************************************************************
2 * vlccontrol.cpp: ActiveX control for VLC
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
6 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
7 * Jean-Paul Saman <jpsaman@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 #include "vlccontrol.h"
31 VLCControl::~VLCControl()
34 _p_typeinfo->Release();
37 HRESULT VLCControl::getTypeInfo(void)
40 if( NULL == _p_typeinfo )
44 hr = _p_instance->getTypeLib(LOCALE_USER_DEFAULT, &p_typelib);
47 hr = p_typelib->GetTypeInfoOfGuid(IID_IVLCControl, &_p_typeinfo);
58 STDMETHODIMP VLCControl::GetTypeInfoCount(UINT* pctInfo)
63 if( SUCCEEDED(getTypeInfo()) )
71 STDMETHODIMP VLCControl::GetTypeInfo(UINT iTInfo, LCID lcid, LPTYPEINFO* ppTInfo)
76 if( SUCCEEDED(getTypeInfo()) )
78 _p_typeinfo->AddRef();
79 *ppTInfo = _p_typeinfo;
86 STDMETHODIMP VLCControl::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames,
87 UINT cNames, LCID lcid, DISPID* rgDispID)
89 if( SUCCEEDED(getTypeInfo()) )
91 return DispGetIDsOfNames(_p_typeinfo, rgszNames, cNames, rgDispID);
96 STDMETHODIMP VLCControl::Invoke(DISPID dispIdMember, REFIID riid,
97 LCID lcid, WORD wFlags, DISPPARAMS* pDispParams,
98 VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
100 if( SUCCEEDED(getTypeInfo()) )
102 return DispInvoke(this, _p_typeinfo, dispIdMember, wFlags, pDispParams,
103 pVarResult, pExcepInfo, puArgErr);
108 STDMETHODIMP VLCControl::get_Visible(VARIANT_BOOL *isVisible)
110 if( NULL == isVisible )
113 *isVisible = _p_instance->getVisible() ? VARIANT_TRUE : VARIANT_FALSE;
118 STDMETHODIMP VLCControl::put_Visible(VARIANT_BOOL isVisible)
120 _p_instance->setVisible(isVisible != VARIANT_FALSE);
125 STDMETHODIMP VLCControl::play(void)
127 libvlc_exception_t ex;
128 libvlc_exception_init(&ex);
130 _p_instance->playlist_play(&ex);
131 if( libvlc_exception_raised(&ex) )
133 _p_instance->setErrorInfo(IID_IVLCControl,
134 libvlc_exception_get_message(&ex));
135 libvlc_exception_clear(&ex);
138 _p_instance->fireOnPlayEvent();
142 STDMETHODIMP VLCControl::pause(void)
144 libvlc_media_player_t* p_md;
145 HRESULT result = _p_instance->getMD(&p_md);
146 if( SUCCEEDED(result) )
148 libvlc_exception_t ex;
149 libvlc_exception_init(&ex);
151 libvlc_media_player_pause(p_md, &ex);
152 if( libvlc_exception_raised(&ex) )
154 _p_instance->setErrorInfo(IID_IVLCControl,
155 libvlc_exception_get_message(&ex));
156 libvlc_exception_clear(&ex);
159 _p_instance->fireOnPauseEvent();
165 STDMETHODIMP VLCControl::stop(void)
167 libvlc_media_player_t *p_md;
168 HRESULT result = _p_instance->getMD(&p_md);
169 if( SUCCEEDED(result) )
171 libvlc_exception_t ex;
172 libvlc_exception_init(&ex);
174 libvlc_media_player_stop(p_md, &ex);
175 if( libvlc_exception_raised(&ex) )
177 _p_instance->setErrorInfo(IID_IVLCControl,
178 libvlc_exception_get_message(&ex));
179 libvlc_exception_clear(&ex);
184 _p_instance->fireOnStopEvent();
188 STDMETHODIMP VLCControl::get_Playing(VARIANT_BOOL *isPlaying)
190 if( NULL == isPlaying )
193 libvlc_media_player_t *p_md;
194 HRESULT result = _p_instance->getMD(&p_md);
195 if( SUCCEEDED(result) )
197 *isPlaying = libvlc_media_player_is_playing(p_md, NULL) ?
198 VARIANT_TRUE : VARIANT_FALSE;
201 *isPlaying = VARIANT_FALSE;
205 STDMETHODIMP VLCControl::get_Position(float *position)
207 if( NULL == position )
211 libvlc_media_player_t *p_md;
212 HRESULT result = _p_instance->getMD(&p_md);
213 if( SUCCEEDED(result) )
215 libvlc_exception_t ex;
216 libvlc_exception_init(&ex);
218 *position = libvlc_media_player_get_position(p_md, &ex);
219 if( ! libvlc_exception_raised(&ex) )
223 _p_instance->setErrorInfo(IID_IVLCControl,
224 libvlc_exception_get_message(&ex));
225 libvlc_exception_clear(&ex);
231 STDMETHODIMP VLCControl::put_Position(float position)
233 libvlc_media_player_t *p_md;
234 HRESULT result = _p_instance->getMD(&p_md);
235 if( SUCCEEDED(result) )
237 libvlc_exception_t ex;
238 libvlc_exception_init(&ex);
240 libvlc_media_player_set_position(p_md, position, &ex);
241 if( ! libvlc_exception_raised(&ex) )
245 _p_instance->setErrorInfo(IID_IVLCControl,
246 libvlc_exception_get_message(&ex));
247 libvlc_exception_clear(&ex);
253 STDMETHODIMP VLCControl::get_Time(int *seconds)
255 if( NULL == seconds )
259 libvlc_media_player_t *p_md;
260 HRESULT result = _p_instance->getMD(&p_md);
261 if( SUCCEEDED(result) )
263 libvlc_exception_t ex;
264 libvlc_exception_init(&ex);
266 *seconds = libvlc_media_player_get_time(p_md, &ex);
267 if( ! libvlc_exception_raised(&ex) )
271 _p_instance->setErrorInfo(IID_IVLCControl,
272 libvlc_exception_get_message(&ex));
273 libvlc_exception_clear(&ex);
279 STDMETHODIMP VLCControl::put_Time(int seconds)
281 /* setTime function of the plugin sets the time. */
282 _p_instance->setTime(seconds);
286 STDMETHODIMP VLCControl::shuttle(int seconds)
288 libvlc_media_player_t *p_md;
289 HRESULT result = _p_instance->getMD(&p_md);
290 if( SUCCEEDED(result) )
292 libvlc_exception_t ex;
293 libvlc_exception_init(&ex);
295 if( seconds < 0 ) seconds = 0;
296 libvlc_media_player_set_time(p_md, (int64_t)seconds, &ex);
297 if( ! libvlc_exception_raised(&ex) )
301 _p_instance->setErrorInfo(IID_IVLCControl,
302 libvlc_exception_get_message(&ex));
303 libvlc_exception_clear(&ex);
310 STDMETHODIMP VLCControl::fullscreen(void)
312 libvlc_media_player_t *p_md;
313 HRESULT result = _p_instance->getMD(&p_md);
314 if( SUCCEEDED(result) )
316 if( libvlc_media_player_is_playing(p_md, NULL) )
318 libvlc_toggle_fullscreen(p_md, NULL);
324 STDMETHODIMP VLCControl::get_Length(int *seconds)
326 if( NULL == seconds )
330 libvlc_media_player_t *p_md;
331 HRESULT result = _p_instance->getMD(&p_md);
332 if( SUCCEEDED(result) )
334 libvlc_exception_t ex;
335 libvlc_exception_init(&ex);
337 *seconds = (double)libvlc_media_player_get_length(p_md, &ex);
338 if( ! libvlc_exception_raised(&ex) )
342 _p_instance->setErrorInfo(IID_IVLCControl,
343 libvlc_exception_get_message(&ex));
344 libvlc_exception_clear(&ex);
351 STDMETHODIMP VLCControl::playFaster(void)
355 libvlc_media_player_t *p_md;
356 HRESULT result = _p_instance->getMD(&p_md);
358 if( SUCCEEDED(result) )
360 libvlc_exception_t ex;
361 libvlc_exception_init(&ex);
363 if( ! libvlc_exception_raised(&ex) )
365 libvlc_media_player_set_rate(p_md, rate, &ex);
366 if( ! libvlc_exception_raised(&ex) )
371 _p_instance->setErrorInfo(IID_IVLCControl,
372 libvlc_exception_get_message(&ex));
373 libvlc_exception_clear(&ex);
379 STDMETHODIMP VLCControl::playSlower(void)
383 libvlc_media_player_t *p_md;
384 HRESULT result = _p_instance->getMD(&p_md);
385 if( SUCCEEDED(result) )
387 libvlc_exception_t ex;
388 libvlc_exception_init(&ex);
390 libvlc_media_player_set_rate(p_md, rate, &ex);
391 if( ! libvlc_exception_raised(&ex) )
395 _p_instance->setErrorInfo(IID_IVLCControl,
396 libvlc_exception_get_message(&ex));
397 libvlc_exception_clear(&ex);
403 STDMETHODIMP VLCControl::get_Volume(int *volume)
408 *volume = _p_instance->getVolume();
412 STDMETHODIMP VLCControl::put_Volume(int volume)
414 _p_instance->setVolume(volume);
418 STDMETHODIMP VLCControl::toggleMute(void)
420 libvlc_instance_t* p_libvlc;
421 HRESULT result = _p_instance->getVLC(&p_libvlc);
422 if( SUCCEEDED(result) )
424 libvlc_exception_t ex;
425 libvlc_exception_init(&ex);
427 libvlc_audio_toggle_mute(p_libvlc, &ex);
428 if( libvlc_exception_raised(&ex) )
430 _p_instance->setErrorInfo(IID_IVLCControl,
431 libvlc_exception_get_message(&ex));
432 libvlc_exception_clear(&ex);
440 STDMETHODIMP VLCControl::setVariable(BSTR name, VARIANT value)
442 libvlc_instance_t* p_libvlc;
443 HRESULT result = _p_instance->getVLC(&p_libvlc);
444 if( SUCCEEDED(result) )
446 _p_instance->setErrorInfo(IID_IVLCControl,
447 "setVariable() is an unsafe interface to use. "
448 "It has been removed because of security implications." );
453 STDMETHODIMP VLCControl::getVariable(BSTR name, VARIANT *value)
455 libvlc_instance_t* p_libvlc;
456 HRESULT result = _p_instance->getVLC(&p_libvlc);
457 if( SUCCEEDED(result) )
459 _p_instance->setErrorInfo(IID_IVLCControl,
460 "getVariable() is an unsafe interface to use. "
461 "It has been removed because of security implications." );
466 void VLCControl::FreeTargetOptions(char **cOptions, int cOptionCount)
469 if( NULL != cOptions )
471 for( int pos=0; pos<cOptionCount; ++pos )
473 char *cOption = cOptions[pos];
474 if( NULL != cOption )
475 CoTaskMemFree(cOption);
479 CoTaskMemFree(cOptions);
483 static HRESULT parseStringOptions(int codePage, BSTR bstr, char*** cOptions, int *cOptionCount)
485 HRESULT hr = E_INVALIDARG;
486 if( SysStringLen(bstr) > 0 )
489 char *s = CStrFromBSTR(codePage, bstr);
494 char **options = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
499 char *end = val + strlen(val);
502 // skip leading blanks
504 && ((*val == ' ' ) || (*val == '\t')) )
508 // skip till we get a blank character
514 if( ('\'' == c) || ('"' == c) )
516 // skip till end of string
517 while( (val < end) && (*(val++) != c ) );
523 if( nOptions == capacity )
526 char **moreOptions = (char **)CoTaskMemRealloc(options, capacity*sizeof(char*));
529 /* failed to allocate more memory */
531 /* return what we got so far */
532 *cOptionCount = nOptions;
536 options = moreOptions;
539 options[nOptions] = (char *)CoTaskMemAlloc(val-start);
540 if( options[nOptions] )
542 memcpy(options[nOptions], start, val-start);
547 /* failed to allocate memory */
549 /* return what we got so far */
550 *cOptionCount = nOptions;
556 // must be end of string
559 *cOptionCount = nOptions;
569 HRESULT VLCControl::CreateTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
571 HRESULT hr = E_INVALIDARG;
572 if( VT_ERROR == V_VT(options) )
574 if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
576 // optional parameter not set
582 else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
589 else if( VT_DISPATCH == V_VT(options) )
591 // if object is a collection, retrieve enumerator
593 V_VT(&colEnum) = VT_UNKNOWN;
594 hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
597 IEnumVARIANT *enumVar;
598 hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
605 *cOptions = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
606 if( NULL != *cOptions )
608 ZeroMemory(*cOptions, sizeof(char *)*capacity);
609 while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
611 if( VT_BSTR == V_VT(&option) )
613 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
614 (*cOptions)[pos] = cOption;
615 if( NULL != cOption )
618 if( pos == capacity )
620 char **moreOptions = (char **)CoTaskMemRealloc(*cOptions, (capacity+16)*sizeof(char *));
621 if( NULL != moreOptions )
623 ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
625 *cOptions = moreOptions;
632 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
633 E_OUTOFMEMORY : E_INVALIDARG;
638 VariantClear(&option);
643 // free already processed elements
644 FreeTargetOptions(*cOptions, *cOptionCount);
655 // coerce object into a string and parse it
657 VariantInit(&v_name);
658 hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
661 hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
662 VariantClear(&v_name);
666 else if( V_ISARRAY(options) )
669 SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
671 if( SafeArrayGetDim(array) != 1 )
676 SafeArrayGetLBound(array, 1, &lBound);
677 SafeArrayGetUBound(array, 1, &uBound);
679 // have we got any options
680 if( uBound >= lBound )
683 hr = SafeArrayGetVartype(array, &vType);
689 // marshall options into an array of C strings
690 if( VT_VARIANT == vType )
692 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
693 if( NULL == *cOptions )
694 return E_OUTOFMEMORY;
696 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
697 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
700 hr = SafeArrayGetElement(array, &pos, &option);
703 if( VT_BSTR == V_VT(&option) )
705 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
706 (*cOptions)[pos-lBound] = cOption;
707 if( NULL == cOption )
708 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
709 E_OUTOFMEMORY : E_INVALIDARG;
713 VariantClear(&option);
717 else if( VT_BSTR == vType )
719 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
720 if( NULL == *cOptions )
721 return E_OUTOFMEMORY;
723 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
724 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
727 hr = SafeArrayGetElement(array, &pos, &option);
730 char *cOption = CStrFromBSTR(codePage, option);
732 (*cOptions)[pos-lBound] = cOption;
733 if( NULL == cOption )
734 hr = ( SysStringLen(option) > 0 ) ?
735 E_OUTOFMEMORY : E_INVALIDARG;
736 SysFreeString(option);
746 *cOptionCount = pos-lBound;
749 // free already processed elements
750 FreeTargetOptions(*cOptions, *cOptionCount);
761 else if( VT_UNKNOWN == V_VT(options) )
763 // coerce object into a string and parse it
765 VariantInit(&v_name);
766 hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
769 hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
770 VariantClear(&v_name);
773 else if( VT_BSTR == V_VT(options) )
775 hr = parseStringOptions(codePage, V_BSTR(options), cOptions, cOptionCount);
781 ** use VARIANT rather than a SAFEARRAY as argument type
782 ** for compatibility with some scripting language (JScript)
785 STDMETHODIMP VLCControl::addTarget(BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
787 if( 0 == SysStringLen(uri) )
790 libvlc_instance_t *p_libvlc;
791 HRESULT hr = _p_instance->getVLC(&p_libvlc);
794 char *cUri = CStrFromBSTR(CP_UTF8, uri);
796 return E_OUTOFMEMORY;
801 if( FAILED(CreateTargetOptions(CP_UTF8, &options, &cOptions, &cOptionsCount)) )
804 libvlc_exception_t ex;
805 libvlc_exception_init(&ex);
807 position = _p_instance->playlist_add_extended_untrusted(cUri,
808 cOptionsCount, const_cast<const char**>(cOptions), &ex);
810 FreeTargetOptions(cOptions, cOptionsCount);
813 if( libvlc_exception_raised(&ex) )
815 _p_instance->setErrorInfo(IID_IVLCPlaylist,
816 libvlc_exception_get_message(&ex));
817 libvlc_exception_clear(&ex);
819 if( mode & VLCPlayListAppendAndGo )
820 _p_instance->fireOnStopEvent();
824 if( mode & VLCPlayListAppendAndGo )
825 _p_instance->fireOnPlayEvent();
831 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
837 libvlc_instance_t *p_libvlc;
838 HRESULT result = _p_instance->getVLC(&p_libvlc);
839 if( SUCCEEDED(result) )
841 libvlc_exception_t ex;
842 libvlc_exception_init(&ex);
844 *index = _p_instance->playlist_get_current_index(&ex);
845 if( libvlc_exception_raised(&ex) )
847 _p_instance->setErrorInfo(IID_IVLCControl,
848 libvlc_exception_get_message(&ex));
849 libvlc_exception_clear(&ex);
857 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
862 libvlc_exception_t ex;
863 libvlc_exception_init(&ex);
865 *count = _p_instance->playlist_count(&ex);
866 if( libvlc_exception_raised(&ex) )
868 _p_instance->setErrorInfo(IID_IVLCControl,
869 libvlc_exception_get_message(&ex));
870 libvlc_exception_clear(&ex);
876 STDMETHODIMP VLCControl::playlistNext(void)
878 libvlc_instance_t* p_libvlc;
879 HRESULT result = _p_instance->getVLC(&p_libvlc);
880 if( SUCCEEDED(result) )
882 libvlc_exception_t ex;
883 libvlc_exception_init(&ex);
885 _p_instance->playlist_next(&ex);
886 if( libvlc_exception_raised(&ex) )
888 _p_instance->setErrorInfo(IID_IVLCControl,
889 libvlc_exception_get_message(&ex));
890 libvlc_exception_clear(&ex);
898 STDMETHODIMP VLCControl::playlistPrev(void)
900 libvlc_instance_t* p_libvlc;
901 HRESULT result = _p_instance->getVLC(&p_libvlc);
902 if( SUCCEEDED(result) )
904 libvlc_exception_t ex;
905 libvlc_exception_init(&ex);
907 _p_instance->playlist_prev(&ex);
908 if( libvlc_exception_raised(&ex) )
910 _p_instance->setErrorInfo(IID_IVLCControl,
911 libvlc_exception_get_message(&ex));
912 libvlc_exception_clear(&ex);
920 STDMETHODIMP VLCControl::playlistClear(void)
922 libvlc_instance_t* p_libvlc;
923 HRESULT result = _p_instance->getVLC(&p_libvlc);
924 if( SUCCEEDED(result) )
926 libvlc_exception_t ex;
927 libvlc_exception_init(&ex);
929 _p_instance->playlist_clear(&ex);
930 if( libvlc_exception_raised(&ex) )
932 _p_instance->setErrorInfo(IID_IVLCControl,
933 libvlc_exception_get_message(&ex));
934 libvlc_exception_clear(&ex);
942 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
944 if( NULL == version )
947 const char *versionStr = libvlc_get_version();
948 if( NULL != versionStr )
950 *version = BSTRFromCStr(CP_UTF8, versionStr);
951 return (NULL == *version) ? E_OUTOFMEMORY : NOERROR;
957 STDMETHODIMP VLCControl::get_MRL(BSTR *mrl)
962 *mrl = SysAllocStringLen(_p_instance->getMRL(),
963 SysStringLen(_p_instance->getMRL()));
967 STDMETHODIMP VLCControl::put_MRL(BSTR mrl)
969 _p_instance->setMRL(mrl);
974 STDMETHODIMP VLCControl::get_AutoPlay(VARIANT_BOOL *autoplay)
976 if( NULL == autoplay )
979 *autoplay = _p_instance->getAutoPlay() ? VARIANT_TRUE: VARIANT_FALSE;
983 STDMETHODIMP VLCControl::put_AutoPlay(VARIANT_BOOL autoplay)
985 _p_instance->setAutoPlay((VARIANT_FALSE != autoplay) ? TRUE: FALSE);
989 STDMETHODIMP VLCControl::get_AutoLoop(VARIANT_BOOL *autoloop)
991 if( NULL == autoloop )
994 *autoloop = _p_instance->getAutoLoop() ? VARIANT_TRUE: VARIANT_FALSE;
998 STDMETHODIMP VLCControl::put_AutoLoop(VARIANT_BOOL autoloop)
1000 _p_instance->setAutoLoop((VARIANT_FALSE != autoloop) ? TRUE: FALSE);