1 /*****************************************************************************
2 * vlccontrol.cpp: ActiveX control for VLC
3 *****************************************************************************
4 * Copyright (C) 2005-2010 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 _p_instance->playlist_play();
128 _p_instance->fireOnPlayEvent();
132 STDMETHODIMP VLCControl::pause(void)
134 libvlc_media_player_t* p_md;
135 HRESULT result = _p_instance->getMD(&p_md);
136 if( SUCCEEDED(result) )
138 libvlc_media_player_pause(p_md);
139 _p_instance->fireOnPauseEvent();
144 STDMETHODIMP VLCControl::stop(void)
146 libvlc_media_player_t *p_md;
147 HRESULT result = _p_instance->getMD(&p_md);
148 if( SUCCEEDED(result) )
150 libvlc_media_player_stop(p_md);
151 _p_instance->fireOnStopEvent();
156 STDMETHODIMP VLCControl::get_Playing(VARIANT_BOOL *isPlaying)
158 if( NULL == isPlaying )
161 libvlc_media_player_t *p_md;
162 HRESULT result = _p_instance->getMD(&p_md);
163 if( SUCCEEDED(result) )
165 *isPlaying = libvlc_media_player_is_playing(p_md) ?
166 VARIANT_TRUE : VARIANT_FALSE;
167 } else *isPlaying = VARIANT_FALSE;
171 STDMETHODIMP VLCControl::get_Position(float *position)
173 if( NULL == position )
177 libvlc_media_player_t *p_md;
178 HRESULT result = _p_instance->getMD(&p_md);
179 if( SUCCEEDED(result) )
181 *position = libvlc_media_player_get_position(p_md);
186 STDMETHODIMP VLCControl::put_Position(float position)
188 libvlc_media_player_t *p_md;
189 HRESULT result = _p_instance->getMD(&p_md);
190 if( SUCCEEDED(result) )
192 libvlc_media_player_set_position(p_md, position);
197 STDMETHODIMP VLCControl::get_Time(int *seconds)
199 if( NULL == seconds )
203 libvlc_media_player_t *p_md;
204 HRESULT result = _p_instance->getMD(&p_md);
205 if( SUCCEEDED(result) )
207 *seconds = libvlc_media_player_get_time(p_md);
212 STDMETHODIMP VLCControl::put_Time(int seconds)
214 /* setTime function of the plugin sets the time. */
215 _p_instance->setTime(seconds);
219 STDMETHODIMP VLCControl::shuttle(int seconds)
221 libvlc_media_player_t *p_md;
222 HRESULT result = _p_instance->getMD(&p_md);
223 if( SUCCEEDED(result) )
225 if( seconds < 0 ) seconds = 0;
226 libvlc_media_player_set_time(p_md, (int64_t)seconds);
232 STDMETHODIMP VLCControl::fullscreen(void)
234 libvlc_media_player_t *p_md;
235 HRESULT result = _p_instance->getMD(&p_md);
236 if( SUCCEEDED(result) )
238 if( libvlc_media_player_is_playing(p_md) )
240 libvlc_toggle_fullscreen(p_md);
246 STDMETHODIMP VLCControl::get_Length(int *seconds)
248 if( NULL == seconds )
252 libvlc_media_player_t *p_md;
253 HRESULT result = _p_instance->getMD(&p_md);
254 if( SUCCEEDED(result) )
256 *seconds = (double)libvlc_media_player_get_length(p_md);
262 STDMETHODIMP VLCControl::playFaster(void)
266 libvlc_media_player_t *p_md;
267 HRESULT result = _p_instance->getMD(&p_md);
269 if( SUCCEEDED(result) )
271 libvlc_media_player_set_rate(p_md, rate);
276 STDMETHODIMP VLCControl::playSlower(void)
280 libvlc_media_player_t *p_md;
281 HRESULT result = _p_instance->getMD(&p_md);
282 if( SUCCEEDED(result) )
284 libvlc_media_player_set_rate(p_md, rate);
289 STDMETHODIMP VLCControl::get_Volume(int *volume)
294 *volume = _p_instance->getVolume();
298 STDMETHODIMP VLCControl::put_Volume(int volume)
300 _p_instance->setVolume(volume);
304 STDMETHODIMP VLCControl::toggleMute(void)
306 libvlc_media_player_t *p_md;
307 HRESULT result = _p_instance->getMD(&p_md);
308 if( SUCCEEDED(result) )
309 libvlc_audio_toggle_mute(p_md);
313 STDMETHODIMP VLCControl::setVariable(BSTR name, VARIANT value)
315 libvlc_instance_t* p_libvlc;
316 HRESULT result = _p_instance->getVLC(&p_libvlc);
317 if( SUCCEEDED(result) )
319 _p_instance->setErrorInfo(IID_IVLCControl,
320 "setVariable() is an unsafe interface to use. "
321 "It has been removed because of security implications." );
326 STDMETHODIMP VLCControl::getVariable(BSTR name, VARIANT *value)
328 libvlc_instance_t* p_libvlc;
329 HRESULT result = _p_instance->getVLC(&p_libvlc);
330 if( SUCCEEDED(result) )
332 _p_instance->setErrorInfo(IID_IVLCControl,
333 "getVariable() is an unsafe interface to use. "
334 "It has been removed because of security implications." );
339 void VLCControl::FreeTargetOptions(char **cOptions, int cOptionCount)
342 if( NULL != cOptions )
344 for( int pos=0; pos<cOptionCount; ++pos )
346 char *cOption = cOptions[pos];
347 if( NULL != cOption )
348 CoTaskMemFree(cOption);
352 CoTaskMemFree(cOptions);
356 static HRESULT parseStringOptions(int codePage, BSTR bstr, char*** cOptions, int *cOptionCount)
358 HRESULT hr = E_INVALIDARG;
359 if( SysStringLen(bstr) > 0 )
362 char *s = CStrFromBSTR(codePage, bstr);
367 char **options = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
372 char *end = val + strlen(val);
375 // skip leading blanks
377 && ((*val == ' ' ) || (*val == '\t')) )
381 // skip till we get a blank character
387 if( ('\'' == c) || ('"' == c) )
389 // skip till end of string
390 while( (val < end) && (*(val++) != c ) );
396 if( nOptions == capacity )
399 char **moreOptions = (char **)CoTaskMemRealloc(options, capacity*sizeof(char*));
402 /* failed to allocate more memory */
404 /* return what we got so far */
405 *cOptionCount = nOptions;
409 options = moreOptions;
412 options[nOptions] = (char *)CoTaskMemAlloc(val-start);
413 if( options[nOptions] )
415 memcpy(options[nOptions], start, val-start);
420 /* failed to allocate memory */
422 /* return what we got so far */
423 *cOptionCount = nOptions;
429 // must be end of string
432 *cOptionCount = nOptions;
442 HRESULT VLCControl::CreateTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
444 HRESULT hr = E_INVALIDARG;
445 if( VT_ERROR == V_VT(options) )
447 if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
449 // optional parameter not set
455 else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
462 else if( VT_DISPATCH == V_VT(options) )
464 // if object is a collection, retrieve enumerator
466 V_VT(&colEnum) = VT_UNKNOWN;
467 hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
470 IEnumVARIANT *enumVar;
471 hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
478 *cOptions = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
479 if( NULL != *cOptions )
481 ZeroMemory(*cOptions, sizeof(char *)*capacity);
482 while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
484 if( VT_BSTR == V_VT(&option) )
486 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
487 (*cOptions)[pos] = cOption;
488 if( NULL != cOption )
491 if( pos == capacity )
493 char **moreOptions = (char **)CoTaskMemRealloc(*cOptions, (capacity+16)*sizeof(char *));
494 if( NULL != moreOptions )
496 ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
498 *cOptions = moreOptions;
505 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
506 E_OUTOFMEMORY : E_INVALIDARG;
511 VariantClear(&option);
516 // free already processed elements
517 FreeTargetOptions(*cOptions, *cOptionCount);
528 // coerce object into a string and parse it
530 VariantInit(&v_name);
531 hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
534 hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
535 VariantClear(&v_name);
539 else if( V_ISARRAY(options) )
542 SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
544 if( SafeArrayGetDim(array) != 1 )
549 SafeArrayGetLBound(array, 1, &lBound);
550 SafeArrayGetUBound(array, 1, &uBound);
552 // have we got any options
553 if( uBound >= lBound )
556 hr = SafeArrayGetVartype(array, &vType);
562 // marshall options into an array of C strings
563 if( VT_VARIANT == vType )
565 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
566 if( NULL == *cOptions )
567 return E_OUTOFMEMORY;
569 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
570 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
573 hr = SafeArrayGetElement(array, &pos, &option);
576 if( VT_BSTR == V_VT(&option) )
578 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
579 (*cOptions)[pos-lBound] = cOption;
580 if( NULL == cOption )
581 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
582 E_OUTOFMEMORY : E_INVALIDARG;
586 VariantClear(&option);
590 else if( VT_BSTR == vType )
592 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
593 if( NULL == *cOptions )
594 return E_OUTOFMEMORY;
596 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
597 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
600 hr = SafeArrayGetElement(array, &pos, &option);
603 char *cOption = CStrFromBSTR(codePage, option);
605 (*cOptions)[pos-lBound] = cOption;
606 if( NULL == cOption )
607 hr = ( SysStringLen(option) > 0 ) ?
608 E_OUTOFMEMORY : E_INVALIDARG;
609 SysFreeString(option);
619 *cOptionCount = pos-lBound;
622 // free already processed elements
623 FreeTargetOptions(*cOptions, *cOptionCount);
634 else if( VT_UNKNOWN == V_VT(options) )
636 // coerce object into a string and parse it
638 VariantInit(&v_name);
639 hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
642 hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
643 VariantClear(&v_name);
646 else if( VT_BSTR == V_VT(options) )
648 hr = parseStringOptions(codePage, V_BSTR(options), cOptions, cOptionCount);
654 ** use VARIANT rather than a SAFEARRAY as argument type
655 ** for compatibility with some scripting language (JScript)
658 STDMETHODIMP VLCControl::addTarget(BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
660 if( 0 == SysStringLen(uri) )
663 libvlc_instance_t *p_libvlc;
664 HRESULT hr = _p_instance->getVLC(&p_libvlc);
667 char *cUri = CStrFromBSTR(CP_UTF8, uri);
669 return E_OUTOFMEMORY;
674 if( FAILED(CreateTargetOptions(CP_UTF8, &options, &cOptions, &cOptionsCount)) )
677 position = _p_instance->playlist_add_extended_untrusted(cUri,
678 cOptionsCount, const_cast<const char**>(cOptions));
680 FreeTargetOptions(cOptions, cOptionsCount);
685 if( mode & VLCPlayListAppendAndGo )
686 _p_instance->fireOnPlayEvent();
690 if( mode & VLCPlayListAppendAndGo )
691 _p_instance->fireOnStopEvent();
697 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
703 libvlc_instance_t *p_libvlc;
704 HRESULT result = _p_instance->getVLC(&p_libvlc);
705 if( SUCCEEDED(result) )
707 *index = _p_instance->playlist_get_current_index();
712 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
717 *count = _p_instance->playlist_count();
721 STDMETHODIMP VLCControl::playlistNext(void)
723 libvlc_instance_t* p_libvlc;
724 HRESULT result = _p_instance->getVLC(&p_libvlc);
725 if( SUCCEEDED(result) )
727 _p_instance->playlist_next();
732 STDMETHODIMP VLCControl::playlistPrev(void)
734 libvlc_instance_t* p_libvlc;
735 HRESULT result = _p_instance->getVLC(&p_libvlc);
736 if( SUCCEEDED(result) )
738 _p_instance->playlist_prev();
743 STDMETHODIMP VLCControl::playlistClear(void)
745 libvlc_instance_t* p_libvlc;
746 HRESULT result = _p_instance->getVLC(&p_libvlc);
747 if( SUCCEEDED(result) )
749 _p_instance->playlist_clear();
754 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
756 if( NULL == version )
759 const char *versionStr = libvlc_get_version();
760 if( NULL != versionStr )
762 *version = BSTRFromCStr(CP_UTF8, versionStr);
763 return (NULL == *version) ? E_OUTOFMEMORY : NOERROR;
769 STDMETHODIMP VLCControl::get_MRL(BSTR *mrl)
774 *mrl = SysAllocStringLen(_p_instance->getMRL(),
775 SysStringLen(_p_instance->getMRL()));
779 STDMETHODIMP VLCControl::put_MRL(BSTR mrl)
781 _p_instance->setMRL(mrl);
786 STDMETHODIMP VLCControl::get_AutoPlay(VARIANT_BOOL *autoplay)
788 if( NULL == autoplay )
791 *autoplay = _p_instance->getAutoPlay() ? VARIANT_TRUE: VARIANT_FALSE;
795 STDMETHODIMP VLCControl::put_AutoPlay(VARIANT_BOOL autoplay)
797 _p_instance->setAutoPlay((VARIANT_FALSE != autoplay) ? TRUE: FALSE);
801 STDMETHODIMP VLCControl::get_AutoLoop(VARIANT_BOOL *autoloop)
803 if( NULL == autoloop )
806 *autoloop = _p_instance->getAutoLoop() ? VARIANT_TRUE: VARIANT_FALSE;
810 STDMETHODIMP VLCControl::put_AutoLoop(VARIANT_BOOL autoloop)
812 _p_instance->setAutoLoop((VARIANT_FALSE != autoloop) ? TRUE: FALSE);