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., 59 Temple Place - Suite 330, Boston, MA 02111, 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();
129 if( ! _p_instance->isInPlaceActive() )
132 ** object has not yet been activated. try doing it by ourself
133 ** if parent container is known
135 LPOLEOBJECT p_oleobject;
136 if( SUCCEEDED(QueryInterface(IID_IOleObject, (LPVOID *)&p_oleobject)) )
138 LPOLECLIENTSITE p_clientsite;
139 if( SUCCEEDED(p_oleobject->GetClientSite(&p_clientsite)
140 && (NULL != p_clientsite)) )
142 p_oleobject->DoVerb(OLEIVERB_INPLACEACTIVATE,
143 NULL, p_clientsite, 0, NULL, NULL);
144 p_clientsite->Release();
146 p_oleobject->Release();
150 _p_instance->fireOnPlayEvent();
156 STDMETHODIMP VLCControl::pause(void)
158 int i_vlc = _p_instance->getVLCObject();
162 _p_instance->fireOnPauseEvent();
168 STDMETHODIMP VLCControl::stop(void)
170 int i_vlc = _p_instance->getVLCObject();
174 _p_instance->fireOnStopEvent();
180 STDMETHODIMP VLCControl::get_Playing(VARIANT_BOOL *isPlaying)
182 if( NULL == isPlaying )
185 int i_vlc = _p_instance->getVLCObject();
188 *isPlaying = VLC_IsPlaying(i_vlc) ? VARIANT_TRUE : VARIANT_FALSE;
191 *isPlaying = VARIANT_FALSE;
195 STDMETHODIMP VLCControl::put_Playing(VARIANT_BOOL isPlaying)
197 int i_vlc = _p_instance->getVLCObject();
200 if( VARIANT_FALSE == isPlaying )
202 if( VLC_IsPlaying(i_vlc) )
207 if( ! VLC_IsPlaying(i_vlc) )
215 STDMETHODIMP VLCControl::get_Position(float *position)
217 if( NULL == position )
220 int i_vlc = _p_instance->getVLCObject();
223 *position = VLC_PositionGet(i_vlc);
230 STDMETHODIMP VLCControl::put_Position(float position)
232 int i_vlc = _p_instance->getVLCObject();
235 VLC_PositionSet(i_vlc, position);
241 STDMETHODIMP VLCControl::get_Time(int *seconds)
243 if( NULL == seconds )
246 int i_vlc = _p_instance->getVLCObject();
249 *seconds = VLC_TimeGet(i_vlc);
256 STDMETHODIMP VLCControl::put_Time(int seconds)
258 int i_vlc = _p_instance->getVLCObject();
261 VLC_TimeSet(i_vlc, seconds, VLC_FALSE);
267 STDMETHODIMP VLCControl::shuttle(int seconds)
269 int i_vlc = _p_instance->getVLCObject();
272 VLC_TimeSet(i_vlc, seconds, VLC_TRUE);
278 STDMETHODIMP VLCControl::fullscreen(void)
280 int i_vlc = _p_instance->getVLCObject();
283 VLC_FullScreen(i_vlc);
289 STDMETHODIMP VLCControl::get_Length(int *seconds)
291 if( NULL == seconds )
294 int i_vlc = _p_instance->getVLCObject();
297 *seconds = VLC_LengthGet(i_vlc);
304 STDMETHODIMP VLCControl::playFaster(void)
306 int i_vlc = _p_instance->getVLCObject();
309 VLC_SpeedFaster(i_vlc);
315 STDMETHODIMP VLCControl::playSlower(void)
317 int i_vlc = _p_instance->getVLCObject();
320 VLC_SpeedSlower(i_vlc);
326 STDMETHODIMP VLCControl::get_Volume(int *volume)
331 int i_vlc = _p_instance->getVLCObject();
334 *volume = VLC_VolumeGet(i_vlc);
341 STDMETHODIMP VLCControl::put_Volume(int volume)
343 int i_vlc = _p_instance->getVLCObject();
346 VLC_VolumeSet(i_vlc, volume);
352 STDMETHODIMP VLCControl::toggleMute(void)
354 int i_vlc = _p_instance->getVLCObject();
357 VLC_VolumeMute(i_vlc);
363 STDMETHODIMP VLCControl::setVariable( BSTR name, VARIANT value)
365 if( 0 == SysStringLen(name) )
368 int i_vlc = _p_instance->getVLCObject();
371 int codePage = _p_instance->getCodePage();
372 char *psz_varname = CStrFromBSTR(codePage, name);
373 if( NULL == psz_varname )
374 return E_OUTOFMEMORY;
376 HRESULT hr = E_INVALIDARG;
380 if( VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type) )
388 hr = VariantChangeType(&value, &arg, 0, VT_BOOL);
390 val.b_bool = (VARIANT_TRUE == V_BOOL(&arg)) ? VLC_TRUE : VLC_FALSE;
393 case VLC_VAR_INTEGER:
395 hr = VariantChangeType(&value, &arg, 0, VT_I4);
397 val.i_int = V_I4(&arg);
401 hr = VariantChangeType(&value, &arg, 0, VT_R4);
403 val.f_float = V_R4(&arg);
409 case VLC_VAR_DIRECTORY:
410 case VLC_VAR_VARIABLE:
411 hr = VariantChangeType(&value, &arg, 0, VT_BSTR);
414 val.psz_string = CStrFromBSTR(codePage, V_BSTR(&arg));
420 // use a double value to represent time (base is expressed in seconds)
421 hr = VariantChangeType(&value, &arg, 0, VT_R8);
423 val.i_time = (signed __int64)(V_R8(&arg)*1000000.0);
427 hr = DISP_E_TYPEMISMATCH;
431 // no defined type, defaults to VARIANT type
433 switch( V_VT(&value) )
436 val.b_bool = (VARIANT_TRUE == V_BOOL(&value)) ? VLC_TRUE : VLC_FALSE;
437 i_type = VLC_VAR_BOOL;
440 val.i_int = V_I4(&value);
441 i_type = VLC_VAR_INTEGER;
444 val.f_float = V_R4(&value);
445 i_type = VLC_VAR_FLOAT;
448 val.psz_string = CStrFromBSTR(codePage, V_BSTR(&value));
449 i_type = VLC_VAR_STRING;
452 // use a double value to represent time (base is expressed in seconds)
453 val.i_time = (signed __int64)(V_R8(&value)*1000000.0);
454 i_type = VLC_VAR_TIME;
457 hr = DISP_E_TYPEMISMATCH;
462 hr = (VLC_SUCCESS == VLC_VariableSet(i_vlc, psz_varname, val)) ? NOERROR : E_FAIL;
464 if( (VLC_VAR_STRING == i_type) && (NULL != val.psz_string) )
465 CoTaskMemFree(val.psz_string);
467 CoTaskMemFree(psz_varname);
474 STDMETHODIMP VLCControl::getVariable( BSTR name, VARIANT *value)
476 if( 0 == SysStringLen(name) )
482 int i_vlc = _p_instance->getVLCObject();
485 int codePage = _p_instance->getCodePage();
486 char *psz_varname = CStrFromBSTR(codePage, name);
487 if( NULL == psz_varname )
488 return E_OUTOFMEMORY;
490 HRESULT hr = E_INVALIDARG;
495 if( (VLC_SUCCESS == VLC_VariableGet(i_vlc, psz_varname, &val))
496 && (VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type)) )
502 V_VT(value) = VT_BOOL;
503 V_BOOL(value) = val.b_bool ? VARIANT_TRUE : VARIANT_FALSE;
506 case VLC_VAR_INTEGER:
509 V_I4(value) = val.i_int;
514 V_R4(value) = val.f_float;
520 case VLC_VAR_DIRECTORY:
521 case VLC_VAR_VARIABLE:
522 V_VT(value) = VT_BSTR;
523 V_BSTR(value) = BSTRFromCStr(codePage, val.psz_string);
524 CoTaskMemFree(val.psz_string);
528 // use a double value to represent time (base is expressed in seconds)
530 V_R8(value) = ((double)val.i_time)/1000000.0;
534 hr = DISP_E_TYPEMISMATCH;
537 CoTaskMemFree(psz_varname);
543 static void freeTargetOptions(char **cOptions, int cOptionCount)
546 if( NULL != cOptions )
548 for( int pos=0; pos<cOptionCount; ++pos )
550 char *cOption = cOptions[pos];
551 if( NULL != cOption )
552 CoTaskMemFree(cOption);
556 CoTaskMemFree(cOptions);
560 static HRESULT createTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
562 HRESULT hr = E_INVALIDARG;
563 if( VT_ERROR == V_VT(options) )
565 if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
567 // optional parameter not set
573 else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
580 else if( VT_DISPATCH == V_VT(options) )
582 // collection parameter
584 V_VT(&colEnum) = VT_UNKNOWN;
585 hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
588 IEnumVARIANT *enumVar;
589 hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
596 *cOptions = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
597 if( NULL != *cOptions )
599 ZeroMemory(*cOptions, sizeof(char *)*capacity);
600 while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
602 if( VT_BSTR == V_VT(&option) )
604 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
605 (*cOptions)[pos] = cOption;
606 if( NULL != cOption )
609 if( pos == capacity )
611 char **moreOptions = (char **)CoTaskMemRealloc(*cOptions, (capacity+16)*sizeof(char *));
612 if( NULL != moreOptions )
614 ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
616 *cOptions = moreOptions;
623 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
624 E_OUTOFMEMORY : E_INVALIDARG;
629 VariantClear(&option);
634 // free already processed elements
635 freeTargetOptions(*cOptions, *cOptionCount);
645 else if( V_ISARRAY(options) )
648 SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
650 if( SafeArrayGetDim(array) != 1 )
655 SafeArrayGetLBound(array, 1, &lBound);
656 SafeArrayGetUBound(array, 1, &uBound);
658 // have we got any options
659 if( uBound > lBound )
662 hr = SafeArrayGetVartype(array, &vType);
668 // marshall options into an array of C strings
669 if( VT_VARIANT == vType )
671 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound));
672 if( NULL == *cOptions )
673 return E_OUTOFMEMORY;
675 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound));
676 for(pos=lBound; SUCCEEDED(hr) && (pos<uBound); ++pos )
679 hr = SafeArrayGetElement(array, &pos, &option);
682 if( VT_BSTR == V_VT(&option) )
684 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
685 (*cOptions)[pos-lBound] = cOption;
686 if( NULL == cOption )
687 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
688 E_OUTOFMEMORY : E_INVALIDARG;
692 VariantClear(&option);
696 else if( VT_BSTR == vType )
698 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound));
699 if( NULL == *cOptions )
700 return E_OUTOFMEMORY;
702 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound));
703 for(pos=lBound; (pos<uBound) && SUCCEEDED(hr); ++pos )
706 hr = SafeArrayGetElement(array, &pos, &option);
709 char *cOption = CStrFromBSTR(codePage, option);
711 (*cOptions)[pos-lBound] = cOption;
712 if( NULL == cOption )
713 hr = ( SysStringLen(option) > 0 ) ?
714 E_OUTOFMEMORY : E_INVALIDARG;
715 SysFreeString(option);
725 *cOptionCount = pos-lBound;
728 // free already processed elements
729 freeTargetOptions(*cOptions, *cOptionCount);
744 ** use VARIANT rather than a SAFEARRAY as argument type
745 ** for compatibility with some scripting language (JScript)
748 STDMETHODIMP VLCControl::addTarget( BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
750 if( 0 == SysStringLen(uri) )
753 HRESULT hr = E_UNEXPECTED;
755 int i_vlc = _p_instance->getVLCObject();
758 int codePage = _p_instance->getCodePage();
759 char *cUri = CStrFromBSTR(codePage, uri);
761 return E_OUTOFMEMORY;
766 if( FAILED(createTargetOptions(codePage, &options, &cOptions, &cOptionsCount)) )
769 if( VLC_SUCCESS <= VLC_AddTarget(i_vlc, cUri, (const char **)cOptions, cOptionsCount, mode, position) )
772 if( mode & VLCPlayListGo )
773 _p_instance->fireOnPlayEvent();
778 if( mode & VLCPlayListGo )
779 _p_instance->fireOnStopEvent();
782 freeTargetOptions(cOptions, cOptionsCount);
788 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
793 int i_vlc = _p_instance->getVLCObject();
796 *index = VLC_PlaylistIndex(i_vlc);
803 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
805 int i_vlc = _p_instance->getVLCObject();
808 *count = VLC_PlaylistNumberOfItems(i_vlc);
815 STDMETHODIMP VLCControl::playlistNext(void)
817 int i_vlc = _p_instance->getVLCObject();
820 VLC_PlaylistNext(i_vlc);
826 STDMETHODIMP VLCControl::playlistPrev(void)
828 int i_vlc = _p_instance->getVLCObject();
831 VLC_PlaylistPrev(i_vlc);
837 STDMETHODIMP VLCControl::playlistClear(void)
839 int i_vlc = _p_instance->getVLCObject();
842 VLC_PlaylistClear(i_vlc);
848 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
850 if( NULL == version )
853 const char *versionStr = VLC_Version();
854 if( NULL != versionStr )
856 *version = BSTRFromCStr(_p_instance->getCodePage(), versionStr);
858 return NULL == *version ? E_OUTOFMEMORY : NOERROR;