1 /*****************************************************************************
2 * vlccontrol.cpp: ActiveX control for VLC
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN
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();
130 _p_instance->fireOnPlayEvent();
136 STDMETHODIMP VLCControl::pause(void)
138 int i_vlc = _p_instance->getVLCObject();
142 _p_instance->fireOnPauseEvent();
148 STDMETHODIMP VLCControl::stop(void)
150 int i_vlc = _p_instance->getVLCObject();
154 _p_instance->fireOnStopEvent();
160 STDMETHODIMP VLCControl::get_Playing(VARIANT_BOOL *isPlaying)
162 if( NULL == isPlaying )
165 int i_vlc = _p_instance->getVLCObject();
168 *isPlaying = VLC_IsPlaying(i_vlc) ? VARIANT_TRUE : VARIANT_FALSE;
171 *isPlaying = VARIANT_FALSE;
175 STDMETHODIMP VLCControl::put_Playing(VARIANT_BOOL isPlaying)
177 int i_vlc = _p_instance->getVLCObject();
180 if( VARIANT_FALSE == isPlaying )
182 if( VLC_IsPlaying(i_vlc) )
187 if( ! VLC_IsPlaying(i_vlc) )
195 STDMETHODIMP VLCControl::get_Position(float *position)
197 if( NULL == position )
200 int i_vlc = _p_instance->getVLCObject();
203 *position = VLC_PositionGet(i_vlc);
210 STDMETHODIMP VLCControl::put_Position(float position)
212 int i_vlc = _p_instance->getVLCObject();
215 VLC_PositionSet(i_vlc, position);
221 STDMETHODIMP VLCControl::get_Time(int *seconds)
223 if( NULL == seconds )
226 int i_vlc = _p_instance->getVLCObject();
229 *seconds = VLC_TimeGet(i_vlc);
236 STDMETHODIMP VLCControl::put_Time(int seconds)
238 int i_vlc = _p_instance->getVLCObject();
241 VLC_TimeSet(i_vlc, seconds, VLC_FALSE);
247 STDMETHODIMP VLCControl::shuttle(int seconds)
249 int i_vlc = _p_instance->getVLCObject();
252 VLC_TimeSet(i_vlc, seconds, VLC_TRUE);
258 STDMETHODIMP VLCControl::fullscreen(void)
260 int i_vlc = _p_instance->getVLCObject();
263 VLC_FullScreen(i_vlc);
269 STDMETHODIMP VLCControl::get_Length(int *seconds)
271 if( NULL == seconds )
274 int i_vlc = _p_instance->getVLCObject();
277 *seconds = VLC_LengthGet(i_vlc);
284 STDMETHODIMP VLCControl::playFaster(void)
286 int i_vlc = _p_instance->getVLCObject();
289 VLC_SpeedFaster(i_vlc);
295 STDMETHODIMP VLCControl::playSlower(void)
297 int i_vlc = _p_instance->getVLCObject();
300 VLC_SpeedSlower(i_vlc);
306 STDMETHODIMP VLCControl::get_Volume(int *volume)
311 int i_vlc = _p_instance->getVLCObject();
314 *volume = VLC_VolumeGet(i_vlc);
321 STDMETHODIMP VLCControl::put_Volume(int volume)
323 int i_vlc = _p_instance->getVLCObject();
326 VLC_VolumeSet(i_vlc, volume);
332 STDMETHODIMP VLCControl::toggleMute(void)
334 int i_vlc = _p_instance->getVLCObject();
337 VLC_VolumeMute(i_vlc);
343 STDMETHODIMP VLCControl::setVariable( BSTR name, VARIANT value)
345 if( 0 == SysStringLen(name) )
348 int i_vlc = _p_instance->getVLCObject();
351 int codePage = _p_instance->getCodePage();
352 char *psz_varname = CStrFromBSTR(codePage, name);
353 if( NULL == psz_varname )
354 return E_OUTOFMEMORY;
356 HRESULT hr = E_INVALIDARG;
358 if( VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type) )
365 hr = DISP_E_TYPEMISMATCH;
370 hr = VariantChangeType(&value, &arg, 0, VT_BOOL);
372 val.b_bool = (VARIANT_TRUE == V_BOOL(&arg)) ? VLC_TRUE : VLC_FALSE;
375 case VLC_VAR_INTEGER:
376 hr = VariantChangeType(&value, &arg, 0, VT_I4);
378 val.i_int = V_I4(&arg);
382 hr = VariantChangeType(&value, &arg, 0, VT_R4);
384 val.f_float = V_R4(&arg);
388 hr = VariantChangeType(&value, &arg, 0, VT_BSTR);
390 val.psz_string = CStrFromBSTR(codePage, V_BSTR(&arg));
397 hr = (VLC_SUCCESS == VLC_VariableSet(i_vlc, psz_varname, val)) ? NOERROR : E_FAIL;
399 if( (VLC_VAR_STRING == i_type) && (NULL != val.psz_string) )
400 free(val.psz_string);
410 STDMETHODIMP VLCControl::getVariable( BSTR name, VARIANT *value)
412 if( 0 == SysStringLen(name) )
418 int i_vlc = _p_instance->getVLCObject();
421 int codePage = _p_instance->getCodePage();
422 char *psz_varname = CStrFromBSTR(codePage, name);
423 if( NULL == psz_varname )
424 return E_OUTOFMEMORY;
426 HRESULT hr = E_INVALIDARG;
431 if( (VLC_SUCCESS == VLC_VariableGet(i_vlc, psz_varname, &val))
432 && (VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type)) )
438 V_VT(value) = VT_BOOL;
439 V_BOOL(value) = val.b_bool ? VARIANT_TRUE : VARIANT_FALSE;
442 case VLC_VAR_INTEGER:
444 V_I4(value) = val.i_int;
449 V_R4(value) = val.f_float;
453 V_VT(value) = VT_BSTR;
454 V_BSTR(value) = BSTRFromCStr(codePage, val.psz_string);
455 free(val.psz_string);
458 hr = DISP_E_TYPEMISMATCH;
467 static void freeTargetOptions(char **cOptions, int cOptionCount)
470 for( long pos=0; pos<cOptionCount; ++pos )
472 char *cOption = cOptions[pos];
473 if( NULL != cOption )
478 if( NULL != cOptions )
482 static HRESULT createTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
484 HRESULT hr = E_INVALIDARG;
485 if( VT_ERROR == V_VT(options) )
487 if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
489 // optional parameter not set
495 else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
502 else if( VT_DISPATCH == V_VT(options) )
504 // collection parameter
506 V_VT(&colEnum) = VT_UNKNOWN;
507 hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
510 IEnumVARIANT *enumVar;
511 hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
518 *cOptions = (char **)malloc(capacity*sizeof(char *));
519 if( NULL != *cOptions )
521 ZeroMemory(*cOptions, sizeof(char *)*capacity);
522 while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
524 if( VT_BSTR == V_VT(&option) )
526 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
527 (*cOptions)[pos] = cOption;
528 if( NULL != cOption )
531 if( pos == capacity )
533 char **moreOptions = (char **)realloc(*cOptions, (capacity+16)*sizeof(char *));
534 if( NULL != moreOptions )
536 ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
538 *cOptions = moreOptions;
550 VariantClear(&option);
555 // free already processed elements
556 freeTargetOptions(*cOptions, *cOptionCount);
565 else if( V_ISARRAY(options) )
568 SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
570 if( SafeArrayGetDim(array) != 1 )
575 SafeArrayGetLBound(array, 1, &lBound);
576 SafeArrayGetUBound(array, 1, &uBound);
578 // have we got any options
579 if( uBound > lBound )
582 HRESULT hr = SafeArrayGetVartype(array, &vType);
588 // marshall options into an array of C strings
589 if( VT_VARIANT == vType )
591 *cOptions = (char **)malloc(sizeof(char *)*(uBound-lBound));
592 if( NULL != options )
593 return E_OUTOFMEMORY;
595 for(pos=lBound; SUCCEEDED(hr) && (pos<uBound); ++pos )
598 hr = SafeArrayGetElement(array, &pos, &option);
601 if( VT_BSTR == V_VT(&option) )
603 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
604 (*cOptions)[pos-lBound] = cOption;
605 if( NULL == cOption )
610 VariantClear(&option);
614 else if( VT_BSTR == vType )
616 *cOptions = (char **)malloc(sizeof(char *)*(uBound-lBound));
617 if( NULL != options )
618 return E_OUTOFMEMORY;
620 ZeroMemory(cOptions, sizeof(char *)*(uBound-lBound));
621 for(pos=lBound; SUCCEEDED(hr) && (pos<uBound); ++pos )
624 hr = SafeArrayGetElement(array, &pos, &option);
627 char *cOption = CStrFromBSTR(codePage, option);
628 (*cOptions)[pos-lBound] = cOption;
629 if( NULL == cOption )
631 SysFreeString(option);
639 *cOptionCount = pos-lBound;
642 // free already processed elements
643 freeTargetOptions(*cOptions, *cOptionCount);
658 ** use VARIANT rather than a SAFEARRAY as argument type
659 ** for compatibility with some scripting language (JScript)
662 STDMETHODIMP VLCControl::addTarget( BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
664 if( 0 == SysStringLen(uri) )
667 HRESULT hr = E_UNEXPECTED;
669 int i_vlc = _p_instance->getVLCObject();
672 int codePage = _p_instance->getCodePage();
673 char *cUri = CStrFromBSTR(codePage, uri);
675 return E_OUTOFMEMORY;
680 if( FAILED(createTargetOptions(codePage, &options, &cOptions, &cOptionsCount)) )
683 if( VLC_SUCCESS <= VLC_AddTarget(i_vlc, cUri, (const char **)cOptions, cOptionsCount, mode, position) )
686 if( mode & VLCPlayListGo )
687 _p_instance->fireOnPlayEvent();
692 if( mode & VLCPlayListGo )
693 _p_instance->fireOnStopEvent();
696 freeTargetOptions(cOptions, cOptionsCount);
702 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
707 int i_vlc = _p_instance->getVLCObject();
710 *index = VLC_PlaylistIndex(i_vlc);
717 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
719 int i_vlc = _p_instance->getVLCObject();
722 *count = VLC_PlaylistNumberOfItems(i_vlc);
728 STDMETHODIMP VLCControl::playlistNext(void)
730 int i_vlc = _p_instance->getVLCObject();
733 VLC_PlaylistNext(i_vlc);
739 STDMETHODIMP VLCControl::playlistPrev(void)
741 int i_vlc = _p_instance->getVLCObject();
744 VLC_PlaylistPrev(i_vlc);
750 STDMETHODIMP VLCControl::playlistClear(void)
752 int i_vlc = _p_instance->getVLCObject();
755 VLC_PlaylistClear(i_vlc);
761 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
763 if( NULL == version )
766 const char *versionStr = VLC_Version();
767 if( NULL != versionStr )
769 *version = BSTRFromCStr(_p_instance->getCodePage(), versionStr);
771 return NULL == *version ? E_OUTOFMEMORY : NOERROR;