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) )
352 libvlc_audio_toggle_mute(p_libvlc);
356 STDMETHODIMP VLCControl::setVariable(BSTR name, VARIANT value)
358 libvlc_instance_t* p_libvlc;
359 HRESULT result = _p_instance->getVLC(&p_libvlc);
360 if( SUCCEEDED(result) )
362 _p_instance->setErrorInfo(IID_IVLCControl,
363 "setVariable() is an unsafe interface to use. "
364 "It has been removed because of security implications." );
369 STDMETHODIMP VLCControl::getVariable(BSTR name, VARIANT *value)
371 libvlc_instance_t* p_libvlc;
372 HRESULT result = _p_instance->getVLC(&p_libvlc);
373 if( SUCCEEDED(result) )
375 _p_instance->setErrorInfo(IID_IVLCControl,
376 "getVariable() is an unsafe interface to use. "
377 "It has been removed because of security implications." );
382 void VLCControl::FreeTargetOptions(char **cOptions, int cOptionCount)
385 if( NULL != cOptions )
387 for( int pos=0; pos<cOptionCount; ++pos )
389 char *cOption = cOptions[pos];
390 if( NULL != cOption )
391 CoTaskMemFree(cOption);
395 CoTaskMemFree(cOptions);
399 static HRESULT parseStringOptions(int codePage, BSTR bstr, char*** cOptions, int *cOptionCount)
401 HRESULT hr = E_INVALIDARG;
402 if( SysStringLen(bstr) > 0 )
405 char *s = CStrFromBSTR(codePage, bstr);
410 char **options = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
415 char *end = val + strlen(val);
418 // skip leading blanks
420 && ((*val == ' ' ) || (*val == '\t')) )
424 // skip till we get a blank character
430 if( ('\'' == c) || ('"' == c) )
432 // skip till end of string
433 while( (val < end) && (*(val++) != c ) );
439 if( nOptions == capacity )
442 char **moreOptions = (char **)CoTaskMemRealloc(options, capacity*sizeof(char*));
445 /* failed to allocate more memory */
447 /* return what we got so far */
448 *cOptionCount = nOptions;
452 options = moreOptions;
455 options[nOptions] = (char *)CoTaskMemAlloc(val-start);
456 if( options[nOptions] )
458 memcpy(options[nOptions], start, val-start);
463 /* failed to allocate memory */
465 /* return what we got so far */
466 *cOptionCount = nOptions;
472 // must be end of string
475 *cOptionCount = nOptions;
485 HRESULT VLCControl::CreateTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
487 HRESULT hr = E_INVALIDARG;
488 if( VT_ERROR == V_VT(options) )
490 if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
492 // optional parameter not set
498 else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
505 else if( VT_DISPATCH == V_VT(options) )
507 // if object is a collection, retrieve enumerator
509 V_VT(&colEnum) = VT_UNKNOWN;
510 hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
513 IEnumVARIANT *enumVar;
514 hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
521 *cOptions = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
522 if( NULL != *cOptions )
524 ZeroMemory(*cOptions, sizeof(char *)*capacity);
525 while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
527 if( VT_BSTR == V_VT(&option) )
529 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
530 (*cOptions)[pos] = cOption;
531 if( NULL != cOption )
534 if( pos == capacity )
536 char **moreOptions = (char **)CoTaskMemRealloc(*cOptions, (capacity+16)*sizeof(char *));
537 if( NULL != moreOptions )
539 ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
541 *cOptions = moreOptions;
548 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
549 E_OUTOFMEMORY : E_INVALIDARG;
554 VariantClear(&option);
559 // free already processed elements
560 FreeTargetOptions(*cOptions, *cOptionCount);
571 // coerce object into a string and parse it
573 VariantInit(&v_name);
574 hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
577 hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
578 VariantClear(&v_name);
582 else if( V_ISARRAY(options) )
585 SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
587 if( SafeArrayGetDim(array) != 1 )
592 SafeArrayGetLBound(array, 1, &lBound);
593 SafeArrayGetUBound(array, 1, &uBound);
595 // have we got any options
596 if( uBound >= lBound )
599 hr = SafeArrayGetVartype(array, &vType);
605 // marshall options into an array of C strings
606 if( VT_VARIANT == vType )
608 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
609 if( NULL == *cOptions )
610 return E_OUTOFMEMORY;
612 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
613 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
616 hr = SafeArrayGetElement(array, &pos, &option);
619 if( VT_BSTR == V_VT(&option) )
621 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
622 (*cOptions)[pos-lBound] = cOption;
623 if( NULL == cOption )
624 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
625 E_OUTOFMEMORY : E_INVALIDARG;
629 VariantClear(&option);
633 else if( VT_BSTR == 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 char *cOption = CStrFromBSTR(codePage, option);
648 (*cOptions)[pos-lBound] = cOption;
649 if( NULL == cOption )
650 hr = ( SysStringLen(option) > 0 ) ?
651 E_OUTOFMEMORY : E_INVALIDARG;
652 SysFreeString(option);
662 *cOptionCount = pos-lBound;
665 // free already processed elements
666 FreeTargetOptions(*cOptions, *cOptionCount);
677 else if( VT_UNKNOWN == V_VT(options) )
679 // coerce object into a string and parse it
681 VariantInit(&v_name);
682 hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
685 hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
686 VariantClear(&v_name);
689 else if( VT_BSTR == V_VT(options) )
691 hr = parseStringOptions(codePage, V_BSTR(options), 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 libvlc_instance_t *p_libvlc;
707 HRESULT hr = _p_instance->getVLC(&p_libvlc);
710 char *cUri = CStrFromBSTR(CP_UTF8, uri);
712 return E_OUTOFMEMORY;
717 if( FAILED(CreateTargetOptions(CP_UTF8, &options, &cOptions, &cOptionsCount)) )
720 libvlc_exception_t ex;
721 libvlc_exception_init(&ex);
723 position = _p_instance->playlist_add_extended_untrusted(cUri,
724 cOptionsCount, const_cast<const char**>(cOptions), &ex);
726 FreeTargetOptions(cOptions, cOptionsCount);
729 hr = exception_bridge(&ex);
732 if( mode & VLCPlayListAppendAndGo )
733 _p_instance->fireOnPlayEvent();
737 if( mode & VLCPlayListAppendAndGo )
738 _p_instance->fireOnStopEvent();
744 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
750 libvlc_instance_t *p_libvlc;
751 HRESULT result = _p_instance->getVLC(&p_libvlc);
752 if( SUCCEEDED(result) )
754 libvlc_exception_t ex;
755 libvlc_exception_init(&ex);
757 *index = _p_instance->playlist_get_current_index(&ex);
758 result = exception_bridge(&ex);
763 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
768 libvlc_exception_t ex;
769 libvlc_exception_init(&ex);
771 *count = _p_instance->playlist_count(&ex);
772 return exception_bridge(&ex);
775 STDMETHODIMP VLCControl::playlistNext(void)
777 libvlc_instance_t* p_libvlc;
778 HRESULT result = _p_instance->getVLC(&p_libvlc);
779 if( SUCCEEDED(result) )
781 libvlc_exception_t ex;
782 libvlc_exception_init(&ex);
784 _p_instance->playlist_next(&ex);
785 result = exception_bridge(&ex);
790 STDMETHODIMP VLCControl::playlistPrev(void)
792 libvlc_instance_t* p_libvlc;
793 HRESULT result = _p_instance->getVLC(&p_libvlc);
794 if( SUCCEEDED(result) )
796 libvlc_exception_t ex;
797 libvlc_exception_init(&ex);
799 _p_instance->playlist_prev(&ex);
800 result = exception_bridge(&ex);
805 STDMETHODIMP VLCControl::playlistClear(void)
807 libvlc_instance_t* p_libvlc;
808 HRESULT result = _p_instance->getVLC(&p_libvlc);
809 if( SUCCEEDED(result) )
811 libvlc_exception_t ex;
812 libvlc_exception_init(&ex);
814 _p_instance->playlist_clear(&ex);
815 result = exception_bridge(&ex);
820 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
822 if( NULL == version )
825 const char *versionStr = libvlc_get_version();
826 if( NULL != versionStr )
828 *version = BSTRFromCStr(CP_UTF8, versionStr);
829 return (NULL == *version) ? E_OUTOFMEMORY : NOERROR;
835 STDMETHODIMP VLCControl::get_MRL(BSTR *mrl)
840 *mrl = SysAllocStringLen(_p_instance->getMRL(),
841 SysStringLen(_p_instance->getMRL()));
845 STDMETHODIMP VLCControl::put_MRL(BSTR mrl)
847 _p_instance->setMRL(mrl);
852 STDMETHODIMP VLCControl::get_AutoPlay(VARIANT_BOOL *autoplay)
854 if( NULL == autoplay )
857 *autoplay = _p_instance->getAutoPlay() ? VARIANT_TRUE: VARIANT_FALSE;
861 STDMETHODIMP VLCControl::put_AutoPlay(VARIANT_BOOL autoplay)
863 _p_instance->setAutoPlay((VARIANT_FALSE != autoplay) ? TRUE: FALSE);
867 STDMETHODIMP VLCControl::get_AutoLoop(VARIANT_BOOL *autoloop)
869 if( NULL == autoloop )
872 *autoloop = _p_instance->getAutoLoop() ? VARIANT_TRUE: VARIANT_FALSE;
876 STDMETHODIMP VLCControl::put_AutoLoop(VARIANT_BOOL autoloop)
878 _p_instance->setAutoLoop((VARIANT_FALSE != autoloop) ? TRUE: FALSE);