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"
28 VLCControl::~VLCControl()
31 _p_typeinfo->Release();
34 HRESULT VLCControl::getTypeInfo(void)
37 if( NULL == _p_typeinfo )
41 HRESULT hr = _p_instance->getTypeLib(LOCALE_USER_DEFAULT, &p_typelib);
44 hr = p_typelib->GetTypeInfoOfGuid(IID_IVLCControl, &_p_typeinfo);
55 STDMETHODIMP VLCControl::GetTypeInfoCount(UINT* pctInfo)
57 if( SUCCEEDED(getTypeInfo()) )
65 STDMETHODIMP VLCControl::GetTypeInfo(UINT iTInfo, LCID lcid, LPTYPEINFO* ppTInfo)
70 if( SUCCEEDED(getTypeInfo()) )
72 _p_typeinfo->AddRef();
73 *ppTInfo = _p_typeinfo;
80 STDMETHODIMP VLCControl::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames,
81 UINT cNames, LCID lcid, DISPID* rgDispID)
83 if( SUCCEEDED(getTypeInfo()) )
85 return DispGetIDsOfNames(_p_typeinfo, rgszNames, cNames, rgDispID);
90 STDMETHODIMP VLCControl::Invoke(DISPID dispIdMember, REFIID riid,
91 LCID lcid, WORD wFlags, DISPPARAMS* pDispParams,
92 VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
94 if( SUCCEEDED(getTypeInfo()) )
96 return DispInvoke(this, _p_typeinfo, dispIdMember, wFlags, pDispParams,
97 pVarResult, pExcepInfo, puArgErr);
102 STDMETHODIMP VLCControl::get_Value(VARIANT *pvarValue)
104 if( NULL == pvarValue )
107 V_VT(pvarValue) = VT_BOOL;
108 return get_Playing(&V_BOOL(pvarValue));
111 STDMETHODIMP VLCControl::put_Value(VARIANT pvarValue)
113 if( VT_BOOL != V_VT(&pvarValue) )
116 HRESULT hr = VariantChangeType(&boolValue, &pvarValue, 0, VT_BOOL);
119 hr = get_Playing(&V_BOOL(&pvarValue));
120 //VariantClear(&boolValue);
124 return get_Playing(&V_BOOL(&pvarValue));
127 STDMETHODIMP VLCControl::get_Visible(VARIANT_BOOL *isVisible)
129 if( NULL == isVisible )
132 *isVisible = _p_instance->getVisible();
137 STDMETHODIMP VLCControl::put_Visible(VARIANT_BOOL isVisible)
139 _p_instance->setVisible(isVisible != VARIANT_FALSE);
144 STDMETHODIMP VLCControl::play(void)
146 int i_vlc = _p_instance->getVLCObject();
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 static void freeTargetOptions(char **cOptions, int cOptionCount)
366 for( long pos=0; pos<cOptionCount; ++pos )
368 char *cOption = cOptions[pos];
369 if( NULL != cOption )
374 if( NULL != cOptions )
378 static HRESULT createTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
380 HRESULT hr = E_INVALIDARG;
381 if( VT_ERROR == V_VT(options) )
383 if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
385 // optional parameter not set
391 else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
398 else if( VT_DISPATCH == V_VT(options) )
400 // collection parameter
402 V_VT(&colEnum) = VT_UNKNOWN;
403 hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
406 IEnumVARIANT *enumVar;
407 hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
414 *cOptions = (char **)malloc(capacity*sizeof(char *));
415 if( NULL != *cOptions )
417 ZeroMemory(*cOptions, sizeof(char *)*capacity);
418 while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
420 if( VT_BSTR == V_VT(&option) )
422 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
423 (*cOptions)[pos] = cOption;
424 if( NULL != cOption )
427 if( pos == capacity )
429 char **moreOptions = (char **)realloc(*cOptions, (capacity+16)*sizeof(char *));
430 if( NULL != moreOptions )
432 ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
434 *cOptions = moreOptions;
446 VariantClear(&option);
451 // free already processed elements
452 freeTargetOptions(*cOptions, *cOptionCount);
461 else if( V_ISARRAY(options) )
464 SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
466 if( SafeArrayGetDim(array) != 1 )
471 SafeArrayGetLBound(array, 1, &lBound);
472 SafeArrayGetUBound(array, 1, &uBound);
474 // have we got any options
475 if( uBound > lBound )
478 HRESULT hr = SafeArrayGetVartype(array, &vType);
484 // marshall options into an array of C strings
485 if( VT_VARIANT == vType )
487 *cOptions = (char **)malloc(sizeof(char *)*(uBound-lBound));
488 if( NULL != options )
489 return E_OUTOFMEMORY;
491 for(pos=lBound; SUCCEEDED(hr) && (pos<uBound); ++pos )
494 hr = SafeArrayGetElement(array, &pos, &option);
497 if( VT_BSTR == V_VT(&option) )
499 char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
500 (*cOptions)[pos-lBound] = cOption;
501 if( NULL == cOption )
506 VariantClear(&option);
510 else if( VT_BSTR == vType )
512 *cOptions = (char **)malloc(sizeof(char *)*(uBound-lBound));
513 if( NULL != options )
514 return E_OUTOFMEMORY;
516 ZeroMemory(cOptions, sizeof(char *)*(uBound-lBound));
517 for(pos=lBound; SUCCEEDED(hr) && (pos<uBound); ++pos )
520 hr = SafeArrayGetElement(array, &pos, &option);
523 char *cOption = CStrFromBSTR(codePage, option);
524 (*cOptions)[pos-lBound] = cOption;
525 if( NULL == cOption )
527 SysFreeString(option);
535 *cOptionCount = pos-lBound;
538 // free already processed elements
539 freeTargetOptions(*cOptions, *cOptionCount);
554 ** use VARIANT rather than a SAFEARRAY as argument type
555 ** for compatibility with some scripting language (JScript)
558 STDMETHODIMP VLCControl::addTarget( BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
563 HRESULT hr = E_UNEXPECTED;
565 int i_vlc = _p_instance->getVLCObject();
568 int codePage = _p_instance->getCodePage();
569 char *cUri = CStrFromBSTR(codePage, uri);
571 return E_OUTOFMEMORY;
576 if( FAILED(createTargetOptions(codePage, &options, &cOptions, &cOptionsCount)) )
579 VLC_AddTarget(i_vlc, cUri, (const char **)cOptions, cOptionsCount, mode, position);
582 freeTargetOptions(cOptions, cOptionsCount);
588 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
593 int i_vlc = _p_instance->getVLCObject();
596 *index = VLC_PlaylistIndex(i_vlc);
603 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
605 int i_vlc = _p_instance->getVLCObject();
608 *count = VLC_PlaylistNumberOfItems(i_vlc);
614 STDMETHODIMP VLCControl::playlistNext(void)
616 int i_vlc = _p_instance->getVLCObject();
619 VLC_PlaylistNext(i_vlc);
625 STDMETHODIMP VLCControl::playlistPrev(void)
627 int i_vlc = _p_instance->getVLCObject();
630 VLC_PlaylistPrev(i_vlc);
636 STDMETHODIMP VLCControl::playlistClear(void)
638 int i_vlc = _p_instance->getVLCObject();
641 VLC_PlaylistClear(i_vlc);
647 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
649 if( NULL == version )
652 const char *versionStr = VLC_Version();
653 if( NULL != versionStr )
655 *version = BSTRFromCStr(_p_instance->getCodePage(), versionStr);
657 return NULL == *version ? E_OUTOFMEMORY : NOERROR;