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 along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 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 HRESULT result = exception_bridge(&ex);
132 if( SUCCEEDED(result) )
133 _p_instance->fireOnPlayEvent();
137 STDMETHODIMP VLCControl::pause(void)
139 libvlc_media_player_t* p_md;
140 HRESULT result = _p_instance->getMD(&p_md);
141 if( SUCCEEDED(result) )
143 libvlc_exception_t ex;
144 libvlc_exception_init(&ex);
146 libvlc_media_player_pause(p_md, &ex);
147 result = exception_bridge(&ex);
148 if( SUCCEEDED(result) )
149 _p_instance->fireOnPauseEvent();
154 STDMETHODIMP VLCControl::stop(void)
156 libvlc_media_player_t *p_md;
157 HRESULT result = _p_instance->getMD(&p_md);
158 if( SUCCEEDED(result) )
160 libvlc_exception_t ex;
161 libvlc_exception_init(&ex);
163 libvlc_media_player_stop(p_md, &ex);
164 result = exception_bridge(&ex);
165 if( SUCCEEDED(result) )
166 _p_instance->fireOnStopEvent();
171 STDMETHODIMP VLCControl::get_Playing(VARIANT_BOOL *isPlaying)
173 if( NULL == isPlaying )
176 libvlc_media_player_t *p_md;
177 HRESULT result = _p_instance->getMD(&p_md);
178 if( SUCCEEDED(result) )
180 *isPlaying = libvlc_media_player_is_playing(p_md, NULL) ?
181 VARIANT_TRUE : VARIANT_FALSE;
182 } else *isPlaying = VARIANT_FALSE;
186 STDMETHODIMP VLCControl::get_Position(float *position)
188 if( NULL == position )
192 libvlc_media_player_t *p_md;
193 HRESULT result = _p_instance->getMD(&p_md);
194 if( SUCCEEDED(result) )
196 libvlc_exception_t ex;
197 libvlc_exception_init(&ex);
199 *position = libvlc_media_player_get_position(p_md, &ex);
200 result = exception_bridge(&ex);
205 STDMETHODIMP VLCControl::put_Position(float position)
207 libvlc_media_player_t *p_md;
208 HRESULT result = _p_instance->getMD(&p_md);
209 if( SUCCEEDED(result) )
211 libvlc_exception_t ex;
212 libvlc_exception_init(&ex);
214 libvlc_media_player_set_position(p_md, position, &ex);
215 result = exception_bridge(&ex);
220 STDMETHODIMP VLCControl::get_Time(int *seconds)
222 if( NULL == seconds )
226 libvlc_media_player_t *p_md;
227 HRESULT result = _p_instance->getMD(&p_md);
228 if( SUCCEEDED(result) )
230 libvlc_exception_t ex;
231 libvlc_exception_init(&ex);
233 *seconds = libvlc_media_player_get_time(p_md, &ex);
234 result = exception_bridge(&ex);
239 STDMETHODIMP VLCControl::put_Time(int seconds)
241 /* setTime function of the plugin sets the time. */
242 _p_instance->setTime(seconds);
246 STDMETHODIMP VLCControl::shuttle(int seconds)
248 libvlc_media_player_t *p_md;
249 HRESULT result = _p_instance->getMD(&p_md);
250 if( SUCCEEDED(result) )
252 libvlc_exception_t ex;
253 libvlc_exception_init(&ex);
255 if( seconds < 0 ) seconds = 0;
256 libvlc_media_player_set_time(p_md, (int64_t)seconds, &ex);
257 result = exception_bridge(&ex);
263 STDMETHODIMP VLCControl::fullscreen(void)
265 libvlc_media_player_t *p_md;
266 HRESULT result = _p_instance->getMD(&p_md);
267 if( SUCCEEDED(result) )
269 if( libvlc_media_player_is_playing(p_md, NULL) )
271 libvlc_toggle_fullscreen(p_md, NULL);
277 STDMETHODIMP VLCControl::get_Length(int *seconds)
279 if( NULL == seconds )
283 libvlc_media_player_t *p_md;
284 HRESULT result = _p_instance->getMD(&p_md);
285 if( SUCCEEDED(result) )
287 libvlc_exception_t ex;
288 libvlc_exception_init(&ex);
290 *seconds = (double)libvlc_media_player_get_length(p_md, &ex);
291 result = exception_bridge(&ex);
297 STDMETHODIMP VLCControl::playFaster(void)
301 libvlc_media_player_t *p_md;
302 HRESULT result = _p_instance->getMD(&p_md);
304 if( SUCCEEDED(result) )
306 libvlc_exception_t ex;
307 libvlc_exception_init(&ex);
309 libvlc_media_player_set_rate(p_md, rate, &ex);
310 result = exception_bridge(&ex);
315 STDMETHODIMP VLCControl::playSlower(void)
319 libvlc_media_player_t *p_md;
320 HRESULT result = _p_instance->getMD(&p_md);
321 if( SUCCEEDED(result) )
323 libvlc_exception_t ex;
324 libvlc_exception_init(&ex);
326 libvlc_media_player_set_rate(p_md, rate, &ex);
327 result = exception_bridge(&ex);
332 STDMETHODIMP VLCControl::get_Volume(int *volume)
337 *volume = _p_instance->getVolume();
341 STDMETHODIMP VLCControl::put_Volume(int volume)
343 _p_instance->setVolume(volume);
347 STDMETHODIMP VLCControl::toggleMute(void)
349 libvlc_instance_t* p_libvlc;
350 HRESULT result = _p_instance->getVLC(&p_libvlc);
351 if( SUCCEEDED(result) )
353 libvlc_exception_t ex;
354 libvlc_exception_init(&ex);
356 libvlc_audio_toggle_mute(p_libvlc, &ex);
357 result = exception_bridge(&ex);
362 STDMETHODIMP VLCControl::setVariable(BSTR name, VARIANT value)
364 libvlc_instance_t* p_libvlc;
365 HRESULT result = _p_instance->getVLC(&p_libvlc);
366 if( SUCCEEDED(result) )
368 _p_instance->setErrorInfo(IID_IVLCControl,
369 "setVariable() is an unsafe interface to use. "
370 "It has been removed because of security implications." );
375 STDMETHODIMP VLCControl::getVariable(BSTR name, VARIANT *value)
377 libvlc_instance_t* p_libvlc;
378 HRESULT result = _p_instance->getVLC(&p_libvlc);
379 if( SUCCEEDED(result) )
381 _p_instance->setErrorInfo(IID_IVLCControl,
382 "getVariable() is an unsafe interface to use. "
383 "It has been removed because of security implications." );
388 void VLCControl::FreeTargetOptions(char **cOptions, int cOptionCount)
391 if( NULL != cOptions )
393 for( int pos=0; pos<cOptionCount; ++pos )
395 char *cOption = cOptions[pos];
396 if( NULL != cOption )
397 CoTaskMemFree(cOption);
401 CoTaskMemFree(cOptions);
405 static HRESULT parseStringOptions(int codePage, BSTR bstr, char*** cOptions, int *cOptionCount)
407 HRESULT hr = E_INVALIDARG;
408 if( SysStringLen(bstr) > 0 )
411 char *s = CStrFromBSTR(codePage, bstr);
416 char **options = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
421 char *end = val + strlen(val);
424 // skip leading blanks
426 && ((*val == ' ' ) || (*val == '\t')) )
430 // skip till we get a blank character
436 if( ('\'' == c) || ('"' == c) )
438 // skip till end of string
439 while( (val < end) && (*(val++) != c ) );
445 if( nOptions == capacity )
448 char **moreOptions = (char **)CoTaskMemRealloc(options, capacity*sizeof(char*));
451 /* failed to allocate more memory */
453 /* return what we got so far */
454 *cOptionCount = nOptions;
458 options = moreOptions;
461 options[nOptions] = (char *)CoTaskMemAlloc(val-start);
462 if( options[nOptions] )
464 memcpy(options[nOptions], start, val-start);
469 /* failed to allocate memory */
471 /* return what we got so far */
472 *cOptionCount = nOptions;
478 // must be end of string
481 *cOptionCount = nOptions;
491 HRESULT VLCControl::CreateTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
493 HRESULT hr = E_INVALIDARG;
494 if( VT_ERROR == V_VT(options) )
496 if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
498 // optional parameter not set
504 else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
511 else if( VT_DISPATCH == V_VT(options) )
513 // if object is a collection, retrieve enumerator
515 V_VT(&colEnum) = VT_UNKNOWN;
516 hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
519 IEnumVARIANT *enumVar;
520 hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
527 *cOptions = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
528 if( NULL != *cOptions )
530 ZeroMemory(*cOptions, sizeof(char *)*capacity);
531 while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
533 if( VT_BSTR == V_VT(&option) )
535 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
536 (*cOptions)[pos] = cOption;
537 if( NULL != cOption )
540 if( pos == capacity )
542 char **moreOptions = (char **)CoTaskMemRealloc(*cOptions, (capacity+16)*sizeof(char *));
543 if( NULL != moreOptions )
545 ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
547 *cOptions = moreOptions;
554 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
555 E_OUTOFMEMORY : E_INVALIDARG;
560 VariantClear(&option);
565 // free already processed elements
566 FreeTargetOptions(*cOptions, *cOptionCount);
577 // coerce object into a string and parse it
579 VariantInit(&v_name);
580 hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
583 hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
584 VariantClear(&v_name);
588 else if( V_ISARRAY(options) )
591 SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
593 if( SafeArrayGetDim(array) != 1 )
598 SafeArrayGetLBound(array, 1, &lBound);
599 SafeArrayGetUBound(array, 1, &uBound);
601 // have we got any options
602 if( uBound >= lBound )
605 hr = SafeArrayGetVartype(array, &vType);
611 // marshall options into an array of C strings
612 if( VT_VARIANT == vType )
614 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
615 if( NULL == *cOptions )
616 return E_OUTOFMEMORY;
618 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
619 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
622 hr = SafeArrayGetElement(array, &pos, &option);
625 if( VT_BSTR == V_VT(&option) )
627 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
628 (*cOptions)[pos-lBound] = cOption;
629 if( NULL == cOption )
630 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
631 E_OUTOFMEMORY : E_INVALIDARG;
635 VariantClear(&option);
639 else if( VT_BSTR == vType )
641 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
642 if( NULL == *cOptions )
643 return E_OUTOFMEMORY;
645 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
646 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
649 hr = SafeArrayGetElement(array, &pos, &option);
652 char *cOption = CStrFromBSTR(codePage, option);
654 (*cOptions)[pos-lBound] = cOption;
655 if( NULL == cOption )
656 hr = ( SysStringLen(option) > 0 ) ?
657 E_OUTOFMEMORY : E_INVALIDARG;
658 SysFreeString(option);
668 *cOptionCount = pos-lBound;
671 // free already processed elements
672 FreeTargetOptions(*cOptions, *cOptionCount);
683 else if( VT_UNKNOWN == V_VT(options) )
685 // coerce object into a string and parse it
687 VariantInit(&v_name);
688 hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
691 hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
692 VariantClear(&v_name);
695 else if( VT_BSTR == V_VT(options) )
697 hr = parseStringOptions(codePage, V_BSTR(options), cOptions, cOptionCount);
703 ** use VARIANT rather than a SAFEARRAY as argument type
704 ** for compatibility with some scripting language (JScript)
707 STDMETHODIMP VLCControl::addTarget(BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
709 if( 0 == SysStringLen(uri) )
712 libvlc_instance_t *p_libvlc;
713 HRESULT hr = _p_instance->getVLC(&p_libvlc);
716 char *cUri = CStrFromBSTR(CP_UTF8, uri);
718 return E_OUTOFMEMORY;
723 if( FAILED(CreateTargetOptions(CP_UTF8, &options, &cOptions, &cOptionsCount)) )
726 libvlc_exception_t ex;
727 libvlc_exception_init(&ex);
729 position = _p_instance->playlist_add_extended_untrusted(cUri,
730 cOptionsCount, const_cast<const char**>(cOptions), &ex);
732 FreeTargetOptions(cOptions, cOptionsCount);
735 hr = exception_bridge(&ex);
738 if( mode & VLCPlayListAppendAndGo )
739 _p_instance->fireOnPlayEvent();
743 if( mode & VLCPlayListAppendAndGo )
744 _p_instance->fireOnStopEvent();
750 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
756 libvlc_instance_t *p_libvlc;
757 HRESULT result = _p_instance->getVLC(&p_libvlc);
758 if( SUCCEEDED(result) )
760 libvlc_exception_t ex;
761 libvlc_exception_init(&ex);
763 *index = _p_instance->playlist_get_current_index(&ex);
764 result = exception_bridge(&ex);
769 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
774 libvlc_exception_t ex;
775 libvlc_exception_init(&ex);
777 *count = _p_instance->playlist_count(&ex);
778 return exception_bridge(&ex);
781 STDMETHODIMP VLCControl::playlistNext(void)
783 libvlc_instance_t* p_libvlc;
784 HRESULT result = _p_instance->getVLC(&p_libvlc);
785 if( SUCCEEDED(result) )
787 libvlc_exception_t ex;
788 libvlc_exception_init(&ex);
790 _p_instance->playlist_next(&ex);
791 result = exception_bridge(&ex);
796 STDMETHODIMP VLCControl::playlistPrev(void)
798 libvlc_instance_t* p_libvlc;
799 HRESULT result = _p_instance->getVLC(&p_libvlc);
800 if( SUCCEEDED(result) )
802 libvlc_exception_t ex;
803 libvlc_exception_init(&ex);
805 _p_instance->playlist_prev(&ex);
806 result = exception_bridge(&ex);
811 STDMETHODIMP VLCControl::playlistClear(void)
813 libvlc_instance_t* p_libvlc;
814 HRESULT result = _p_instance->getVLC(&p_libvlc);
815 if( SUCCEEDED(result) )
817 libvlc_exception_t ex;
818 libvlc_exception_init(&ex);
820 _p_instance->playlist_clear(&ex);
821 result = exception_bridge(&ex);
826 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
828 if( NULL == version )
831 const char *versionStr = libvlc_get_version();
832 if( NULL != versionStr )
834 *version = BSTRFromCStr(CP_UTF8, versionStr);
835 return (NULL == *version) ? E_OUTOFMEMORY : NOERROR;
841 STDMETHODIMP VLCControl::get_MRL(BSTR *mrl)
846 *mrl = SysAllocStringLen(_p_instance->getMRL(),
847 SysStringLen(_p_instance->getMRL()));
851 STDMETHODIMP VLCControl::put_MRL(BSTR mrl)
853 _p_instance->setMRL(mrl);
858 STDMETHODIMP VLCControl::get_AutoPlay(VARIANT_BOOL *autoplay)
860 if( NULL == autoplay )
863 *autoplay = _p_instance->getAutoPlay() ? VARIANT_TRUE: VARIANT_FALSE;
867 STDMETHODIMP VLCControl::put_AutoPlay(VARIANT_BOOL autoplay)
869 _p_instance->setAutoPlay((VARIANT_FALSE != autoplay) ? TRUE: FALSE);
873 STDMETHODIMP VLCControl::get_AutoLoop(VARIANT_BOOL *autoloop)
875 if( NULL == autoloop )
878 *autoloop = _p_instance->getAutoLoop() ? VARIANT_TRUE: VARIANT_FALSE;
882 STDMETHODIMP VLCControl::put_AutoLoop(VARIANT_BOOL autoloop)
884 _p_instance->setAutoLoop((VARIANT_FALSE != autoloop) ? TRUE: FALSE);