]> git.sesse.net Git - vlc/blob - activex/vlccontrol.cpp
Improved compatibility
[vlc] / activex / vlccontrol.cpp
1 /*****************************************************************************
2  * vlccontrol.cpp: ActiveX control for VLC
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  *
6  * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 #include "plugin.h"
24 #include "vlccontrol.h"
25
26 #include "utils.h"
27
28 using namespace std;
29
30 VLCControl::~VLCControl()
31 {
32     if( _p_typeinfo )
33         _p_typeinfo->Release();
34 };
35
36 HRESULT VLCControl::getTypeInfo(void)
37 {
38     HRESULT hr = NOERROR;
39     if( NULL == _p_typeinfo )
40     {
41         ITypeLib *p_typelib;
42
43         HRESULT hr = _p_instance->getTypeLib(LOCALE_USER_DEFAULT, &p_typelib);
44         if( SUCCEEDED(hr) )
45         {
46             hr = p_typelib->GetTypeInfoOfGuid(IID_IVLCControl, &_p_typeinfo);
47             if( FAILED(hr) )
48             {
49                 _p_typeinfo = NULL;
50             }
51             p_typelib->Release();
52         }
53     }
54     return hr;
55 };
56
57 STDMETHODIMP VLCControl::GetTypeInfoCount(UINT* pctInfo)
58 {
59     if( NULL == pctInfo )
60         return E_INVALIDARG;
61
62     if( SUCCEEDED(getTypeInfo()) )
63         *pctInfo = 1;
64     else
65         *pctInfo = 0;
66
67     return NOERROR;
68 };
69
70 STDMETHODIMP VLCControl::GetTypeInfo(UINT iTInfo, LCID lcid, LPTYPEINFO* ppTInfo)
71 {
72     if( NULL == ppTInfo )
73         return E_INVALIDARG;
74
75     if( SUCCEEDED(getTypeInfo()) )
76     {
77         _p_typeinfo->AddRef();
78         *ppTInfo = _p_typeinfo;
79         return NO_ERROR;
80     }
81     *ppTInfo = NULL;
82     return E_NOTIMPL;
83 };
84
85 STDMETHODIMP VLCControl::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, 
86         UINT cNames, LCID lcid, DISPID* rgDispID)
87 {
88     if( SUCCEEDED(getTypeInfo()) )
89     {
90         return DispGetIDsOfNames(_p_typeinfo, rgszNames, cNames, rgDispID);
91     }
92     return E_NOTIMPL;
93 };
94
95 STDMETHODIMP VLCControl::Invoke(DISPID dispIdMember, REFIID riid,
96         LCID lcid, WORD wFlags, DISPPARAMS* pDispParams,
97         VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
98 {
99     if( SUCCEEDED(getTypeInfo()) )
100     {
101         return DispInvoke(this, _p_typeinfo, dispIdMember, wFlags, pDispParams,
102                 pVarResult, pExcepInfo, puArgErr);
103     }
104     return E_NOTIMPL;
105 };
106
107 STDMETHODIMP VLCControl::get_Visible(VARIANT_BOOL *isVisible)
108 {
109     if( NULL == isVisible )
110         return E_POINTER;
111
112     *isVisible = _p_instance->getVisible() ? VARIANT_TRUE : VARIANT_FALSE;
113
114     return NOERROR;
115 };
116         
117 STDMETHODIMP VLCControl::put_Visible(VARIANT_BOOL isVisible)
118 {
119     _p_instance->setVisible(isVisible != VARIANT_FALSE);
120
121     return NOERROR;
122 };
123
124 STDMETHODIMP VLCControl::play(void)
125 {
126     int i_vlc = _p_instance->getVLCObject();
127     if( i_vlc )
128     {
129         if( ! _p_instance->isInPlaceActive() )
130         {
131             /*
132             ** object has not yet been activated. try doing it by ourself
133             ** if parent container is known
134             */
135             LPOLEOBJECT p_oleobject;
136             if( SUCCEEDED(QueryInterface(IID_IOleObject, (LPVOID *)&p_oleobject)) )
137             {
138                 LPOLECLIENTSITE p_clientsite;
139                 if( SUCCEEDED(p_oleobject->GetClientSite(&p_clientsite)
140                     && (NULL != p_clientsite)) )
141                 {
142                     p_oleobject->DoVerb(OLEIVERB_INPLACEACTIVATE,
143                             NULL, p_clientsite, 0, NULL, NULL);
144                     p_clientsite->Release();
145                 }
146                 p_oleobject->Release();
147             }
148         }
149         VLC_Play(i_vlc);
150         _p_instance->fireOnPlayEvent();
151         return NOERROR;
152     }
153     return E_UNEXPECTED;
154 };
155  
156 STDMETHODIMP VLCControl::pause(void)
157 {
158     int i_vlc = _p_instance->getVLCObject();
159     if( i_vlc )
160     {
161         VLC_Pause(i_vlc);
162         _p_instance->fireOnPauseEvent();
163         return NOERROR;
164     }
165     return E_UNEXPECTED;
166 };
167         
168 STDMETHODIMP VLCControl::stop(void)
169 {
170     int i_vlc = _p_instance->getVLCObject();
171     if( i_vlc )
172     {
173         VLC_Stop(i_vlc);
174         _p_instance->fireOnStopEvent();
175         return NOERROR;
176     }
177     return E_UNEXPECTED;
178 }
179         
180 STDMETHODIMP VLCControl::get_Playing(VARIANT_BOOL *isPlaying)
181 {
182     if( NULL == isPlaying )
183         return E_POINTER;
184
185     int i_vlc = _p_instance->getVLCObject();
186     if( i_vlc )
187     {
188         *isPlaying = VLC_IsPlaying(i_vlc) ? VARIANT_TRUE : VARIANT_FALSE;
189         return NOERROR;
190     }
191     *isPlaying = VARIANT_FALSE;
192     return E_UNEXPECTED;
193 };
194         
195 STDMETHODIMP VLCControl::put_Playing(VARIANT_BOOL isPlaying)
196 {
197     int i_vlc = _p_instance->getVLCObject();
198     if( i_vlc )
199     {
200         if( VARIANT_FALSE == isPlaying )
201         {
202             if( VLC_IsPlaying(i_vlc) )
203                 VLC_Stop(i_vlc);
204         }
205         else
206         {
207             if( ! VLC_IsPlaying(i_vlc) )
208                 VLC_Play(i_vlc);
209         }
210         return NOERROR;
211     }
212     return E_UNEXPECTED;
213 };
214         
215 STDMETHODIMP VLCControl::get_Position(float *position)
216 {
217     if( NULL == position )
218         return E_POINTER;
219
220     int i_vlc = _p_instance->getVLCObject();
221     if( i_vlc )
222     {
223         *position = VLC_PositionGet(i_vlc);
224         return NOERROR;
225     }
226     *position = 0.0f;
227     return E_UNEXPECTED;
228 };
229         
230 STDMETHODIMP VLCControl::put_Position(float position)
231 {
232     int i_vlc = _p_instance->getVLCObject();
233     if( i_vlc )
234     {
235         VLC_PositionSet(i_vlc, position);
236         return NOERROR;
237     }
238     return E_UNEXPECTED;
239 };
240         
241 STDMETHODIMP VLCControl::get_Time(int *seconds)
242 {
243     if( NULL == seconds )
244         return E_POINTER;
245
246     int i_vlc = _p_instance->getVLCObject();
247     if( i_vlc )
248     {
249         *seconds = VLC_TimeGet(i_vlc);
250         return NOERROR;
251     }
252     *seconds = 0;
253     return E_UNEXPECTED;
254 };
255         
256 STDMETHODIMP VLCControl::put_Time(int seconds)
257 {
258     int i_vlc = _p_instance->getVLCObject();
259     if( i_vlc )
260     {
261         VLC_TimeSet(i_vlc, seconds, VLC_FALSE);
262         return NOERROR;
263     }
264     return E_UNEXPECTED;
265 };
266         
267 STDMETHODIMP VLCControl::shuttle(int seconds)
268 {
269     int i_vlc = _p_instance->getVLCObject();
270     if( i_vlc )
271     {
272         VLC_TimeSet(i_vlc, seconds, VLC_TRUE);
273         return NOERROR;
274     }
275     return E_UNEXPECTED;
276 };
277         
278 STDMETHODIMP VLCControl::fullscreen(void)
279 {
280     int i_vlc = _p_instance->getVLCObject();
281     if( i_vlc )
282     {
283         VLC_FullScreen(i_vlc);
284         return NOERROR;
285     }
286     return E_UNEXPECTED;
287 };
288         
289 STDMETHODIMP VLCControl::get_Length(int *seconds)
290 {
291     if( NULL == seconds )
292         return E_POINTER;
293
294     int i_vlc = _p_instance->getVLCObject();
295     if( i_vlc )
296     {
297         *seconds = VLC_LengthGet(i_vlc);
298         return NOERROR;
299     }
300     *seconds = 0;
301     return E_UNEXPECTED;
302 };
303         
304 STDMETHODIMP VLCControl::playFaster(void)
305 {
306     int i_vlc = _p_instance->getVLCObject();
307     if( i_vlc )
308     {
309         VLC_SpeedFaster(i_vlc);
310         return NOERROR;
311     }
312     return E_UNEXPECTED;
313 };
314         
315 STDMETHODIMP VLCControl::playSlower(void)
316 {
317     int i_vlc = _p_instance->getVLCObject();
318     if( i_vlc )
319     {
320         VLC_SpeedSlower(i_vlc);
321         return NOERROR;
322     }
323     return E_UNEXPECTED;
324 };
325         
326 STDMETHODIMP VLCControl::get_Volume(int *volume)
327 {
328     if( NULL == volume )
329         return E_POINTER;
330
331     int i_vlc = _p_instance->getVLCObject();
332     if( i_vlc )
333     {
334         *volume  = VLC_VolumeGet(i_vlc);
335         return NOERROR;
336     }
337     *volume = 0;
338     return E_UNEXPECTED;
339 };
340         
341 STDMETHODIMP VLCControl::put_Volume(int volume)
342 {
343     int i_vlc = _p_instance->getVLCObject();
344     if( i_vlc )
345     {
346         VLC_VolumeSet(i_vlc, volume);
347         return NOERROR;
348     }
349     return E_UNEXPECTED;
350 };
351         
352 STDMETHODIMP VLCControl::toggleMute(void)
353 {
354     int i_vlc = _p_instance->getVLCObject();
355     if( i_vlc )
356     {
357         VLC_VolumeMute(i_vlc);
358         return NOERROR;
359     }
360     return E_UNEXPECTED;
361 };
362
363 STDMETHODIMP VLCControl::setVariable( BSTR name, VARIANT value)
364 {
365     if( 0 == SysStringLen(name) )
366         return E_INVALIDARG;
367
368     int i_vlc = _p_instance->getVLCObject();
369     if( i_vlc )
370     {
371         int codePage = _p_instance->getCodePage();
372         char *psz_varname = CStrFromBSTR(codePage, name);
373         if( NULL == psz_varname )
374             return E_OUTOFMEMORY;
375
376         HRESULT hr = E_INVALIDARG;
377         int i_type;
378         vlc_value_t val;
379         
380         if( VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type) )
381         {
382             VARIANT arg;
383             VariantInit(&arg);
384
385             switch( i_type )
386             {
387                 case VLC_VAR_BOOL:
388                     hr = VariantChangeType(&value, &arg, 0, VT_BOOL);
389                     if( SUCCEEDED(hr) )
390                         val.b_bool = (VARIANT_TRUE == V_BOOL(&arg)) ? VLC_TRUE : VLC_FALSE;
391                     break;
392
393                 case VLC_VAR_INTEGER:
394                 case VLC_VAR_HOTKEY:
395                     hr = VariantChangeType(&value, &arg, 0, VT_I4);
396                     if( SUCCEEDED(hr) )
397                         val.i_int = V_I4(&arg);
398                     break;
399
400                 case VLC_VAR_FLOAT:
401                     hr = VariantChangeType(&value, &arg, 0, VT_R4);
402                     if( SUCCEEDED(hr) )
403                         val.f_float = V_R4(&arg);
404                     break;
405
406                 case VLC_VAR_STRING:
407                 case VLC_VAR_MODULE:
408                 case VLC_VAR_FILE:
409                 case VLC_VAR_DIRECTORY:
410                 case VLC_VAR_VARIABLE:
411                     hr = VariantChangeType(&value, &arg, 0, VT_BSTR);
412                     if( SUCCEEDED(hr) )
413                     {
414                         val.psz_string = CStrFromBSTR(codePage, V_BSTR(&arg));
415                         VariantClear(&arg);
416                     }
417                     break;
418
419                 case VLC_VAR_TIME:
420                     // use a double value to represent time (base is expressed in seconds)
421                     hr = VariantChangeType(&value, &arg, 0, VT_R8);
422                     if( SUCCEEDED(hr) )
423                         val.i_time = (signed __int64)(V_R8(&arg)*1000000.0);
424                     break;
425
426                 default:
427                     hr = DISP_E_TYPEMISMATCH;
428             }
429         }
430         else {
431             // no defined type, defaults to VARIANT type
432             hr = NO_ERROR;
433             switch( V_VT(&value) )
434             {
435                 case VT_BOOL:
436                     val.b_bool = (VARIANT_TRUE == V_BOOL(&value)) ? VLC_TRUE : VLC_FALSE;
437                     i_type = VLC_VAR_BOOL;
438                     break;
439                 case VT_I4:
440                     val.i_int = V_I4(&value);
441                     i_type = VLC_VAR_INTEGER;
442                     break;
443                 case VT_R4:
444                     val.f_float = V_R4(&value);
445                     i_type = VLC_VAR_FLOAT;
446                     break;
447                 case VT_BSTR:
448                     val.psz_string = CStrFromBSTR(codePage, V_BSTR(&value));
449                     i_type = VLC_VAR_STRING;
450                     break;
451                 case VT_R8:
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;
455                     break;
456                 default:
457                     hr = DISP_E_TYPEMISMATCH;
458             }
459         }
460         if( SUCCEEDED(hr) )
461         {
462             hr = (VLC_SUCCESS == VLC_VariableSet(i_vlc, psz_varname, val)) ? NOERROR : E_FAIL;
463
464             if( (VLC_VAR_STRING == i_type) && (NULL != val.psz_string) )
465                 CoTaskMemFree(val.psz_string);
466         }
467         CoTaskMemFree(psz_varname);
468
469         return hr;
470     }
471     return E_UNEXPECTED;
472 };
473
474 STDMETHODIMP VLCControl::getVariable( BSTR name, VARIANT *value)
475 {
476     if( 0 == SysStringLen(name) )
477         return E_INVALIDARG;
478
479     if( NULL == value )
480         return E_POINTER;
481
482     int i_vlc = _p_instance->getVLCObject();
483     if( i_vlc )
484     {
485         int codePage = _p_instance->getCodePage();
486         char *psz_varname = CStrFromBSTR(codePage, name);
487         if( NULL == psz_varname )
488             return E_OUTOFMEMORY;
489
490         HRESULT hr = E_INVALIDARG;
491
492         vlc_value_t val;
493         int i_type;
494
495         if( (VLC_SUCCESS == VLC_VariableGet(i_vlc, psz_varname, &val))
496          && (VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type)) )
497         {
498             hr = NOERROR;
499             switch( i_type )
500             {
501                 case VLC_VAR_BOOL:
502                     V_VT(value) = VT_BOOL;
503                     V_BOOL(value) = val.b_bool ? VARIANT_TRUE : VARIANT_FALSE;
504                     break;
505
506                 case VLC_VAR_INTEGER:
507                 case VLC_VAR_HOTKEY:
508                     V_VT(value) = VT_I4;
509                     V_I4(value) = val.i_int;
510                     break;
511
512                 case VLC_VAR_FLOAT:
513                     V_VT(value) = VT_R4;
514                     V_R4(value) = val.f_float;
515                     break;
516
517                 case VLC_VAR_STRING:
518                 case VLC_VAR_MODULE:
519                 case VLC_VAR_FILE:
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);
525                     break;
526
527                 case VLC_VAR_TIME:
528                     // use a double value to represent time (base is expressed in seconds)
529                     V_VT(value) = VT_R8;
530                     V_R8(value) = ((double)val.i_time)/1000000.0;
531                     break;
532
533                 default:
534                     hr = DISP_E_TYPEMISMATCH;
535             }
536         }
537         CoTaskMemFree(psz_varname);
538         return hr;
539     }
540     return E_UNEXPECTED;
541 };
542
543 static void freeTargetOptions(char **cOptions, int cOptionCount)
544 {
545     // clean up 
546     if( NULL != cOptions )
547     {
548         for( int pos=0; pos<cOptionCount; ++pos )
549         {
550             char *cOption = cOptions[pos];
551             if( NULL != cOption )
552                 CoTaskMemFree(cOption);
553             else
554                 break;
555         }
556         CoTaskMemFree(cOptions);
557     }
558 };
559
560 static HRESULT createTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
561 {
562     HRESULT hr = E_INVALIDARG;
563     if( VT_ERROR == V_VT(options) )
564     {
565         if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
566         {
567             // optional parameter not set
568             *cOptions = NULL;
569             *cOptionCount = 0;
570             return NOERROR;
571         }
572     }
573     else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
574     {
575         // null parameter
576         *cOptions = NULL;
577         *cOptionCount = 0;
578         return NOERROR;
579     }
580     else if( VT_DISPATCH == V_VT(options) )
581     {
582         // collection parameter
583         VARIANT colEnum;
584         V_VT(&colEnum) = VT_UNKNOWN;
585         hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
586         if( SUCCEEDED(hr) )
587         {
588             IEnumVARIANT *enumVar;
589             hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
590             if( SUCCEEDED(hr) )
591             {
592                 long pos = 0;
593                 long capacity = 16;
594                 VARIANT option;
595
596                 *cOptions = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
597                 if( NULL != *cOptions )
598                 {
599                     ZeroMemory(*cOptions, sizeof(char *)*capacity);
600                     while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
601                     {
602                         if( VT_BSTR == V_VT(&option) )
603                         {
604                             char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
605                             (*cOptions)[pos] = cOption;
606                             if( NULL != cOption )
607                             {
608                                 ++pos;
609                                 if( pos == capacity )
610                                 {
611                                     char **moreOptions = (char **)CoTaskMemRealloc(*cOptions, (capacity+16)*sizeof(char *));
612                                     if( NULL != moreOptions )
613                                     {
614                                         ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
615                                         capacity += 16;
616                                         *cOptions = moreOptions;
617                                     }
618                                     else
619                                         hr = E_OUTOFMEMORY;
620                                 }
621                             }
622                             else
623                                 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
624                                     E_OUTOFMEMORY : E_INVALIDARG;
625                         }
626                         else
627                             hr = E_INVALIDARG;
628
629                         VariantClear(&option);
630                     }
631                     *cOptionCount = pos;
632                     if( FAILED(hr) )
633                     {
634                         // free already processed elements
635                         freeTargetOptions(*cOptions, *cOptionCount);
636                     }
637                 }
638                 else
639                     hr = E_OUTOFMEMORY;
640
641                 enumVar->Release();
642             }
643         }
644     }
645     else if( V_ISARRAY(options) )
646     {
647         // array parameter
648         SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
649
650         if( SafeArrayGetDim(array) != 1 )
651             return E_INVALIDARG;
652
653         long lBound = 0;
654         long uBound = 0;
655         SafeArrayGetLBound(array, 1, &lBound);
656         SafeArrayGetUBound(array, 1, &uBound);
657
658         // have we got any options
659         if( uBound > lBound )
660         {
661             VARTYPE vType;
662             hr = SafeArrayGetVartype(array, &vType);
663             if( FAILED(hr) )
664                 return hr;
665
666             long pos;
667
668             // marshall options into an array of C strings
669             if( VT_VARIANT == vType )
670             {
671                 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound));
672                 if( NULL == *cOptions )
673                     return E_OUTOFMEMORY;
674
675                 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound));
676                 for(pos=lBound; SUCCEEDED(hr) && (pos<uBound); ++pos )
677                 {
678                     VARIANT option;
679                     hr = SafeArrayGetElement(array, &pos, &option);
680                     if( SUCCEEDED(hr) )
681                     {
682                         if( VT_BSTR == V_VT(&option) ) 
683                         {
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;
689                         }
690                         else
691                             hr = E_INVALIDARG;
692                         VariantClear(&option);
693                     }
694                 }
695             }
696             else if( VT_BSTR == vType )
697             {
698                 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound));
699                 if( NULL == *cOptions )
700                     return E_OUTOFMEMORY;
701
702                 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound));
703                 for(pos=lBound; (pos<uBound) && SUCCEEDED(hr); ++pos )
704                 {
705                     BSTR option;
706                     hr = SafeArrayGetElement(array, &pos, &option);
707                     if( SUCCEEDED(hr) )
708                     {
709                         char *cOption = CStrFromBSTR(codePage, option);
710
711                         (*cOptions)[pos-lBound] = cOption;
712                         if( NULL == cOption )
713                             hr = ( SysStringLen(option) > 0 ) ?
714                                 E_OUTOFMEMORY : E_INVALIDARG;
715                         SysFreeString(option);
716                     }
717                 }
718             }
719             else 
720             {
721                 // unsupported type
722                 return E_INVALIDARG;
723             }
724
725             *cOptionCount = pos-lBound;
726             if( FAILED(hr) )
727             {
728                 // free already processed elements
729                 freeTargetOptions(*cOptions, *cOptionCount);
730             }
731         }
732         else
733         {
734             // empty array
735             *cOptions = NULL;
736             *cOptionCount = 0;
737             return NOERROR;
738         }
739     }
740     return hr;
741 };
742
743 /*
744 ** use VARIANT rather than a SAFEARRAY as argument type
745 ** for compatibility with some scripting language (JScript)
746 */
747
748 STDMETHODIMP VLCControl::addTarget( BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
749 {
750     if( 0 == SysStringLen(uri) )
751         return E_INVALIDARG;
752
753     HRESULT hr = E_UNEXPECTED;
754
755     int i_vlc = _p_instance->getVLCObject();
756     if( i_vlc )
757     {
758         int codePage = _p_instance->getCodePage();
759         char *cUri = CStrFromBSTR(codePage, uri);
760         if( NULL == cUri )
761             return E_OUTOFMEMORY;
762
763         int cOptionsCount;
764         char **cOptions;
765
766         if( FAILED(createTargetOptions(codePage, &options, &cOptions, &cOptionsCount)) )
767             return E_INVALIDARG;
768
769         if( VLC_SUCCESS <= VLC_AddTarget(i_vlc, cUri, (const char **)cOptions, cOptionsCount, mode, position) )
770         {
771             hr = NOERROR;
772             if( mode & VLCPlayListGo )
773                 _p_instance->fireOnPlayEvent();
774         }
775         else
776         {
777             hr = E_FAIL;
778             if( mode & VLCPlayListGo )
779                 _p_instance->fireOnStopEvent();
780         }
781
782         freeTargetOptions(cOptions, cOptionsCount);
783         CoTaskMemFree(cUri);
784     }
785     return hr;
786 };
787         
788 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
789 {
790     if( NULL == index )
791         return E_POINTER;
792
793     int i_vlc = _p_instance->getVLCObject();
794     if( i_vlc )
795     {
796         *index = VLC_PlaylistIndex(i_vlc);
797         return NOERROR;
798     }
799     *index = 0;
800     return E_UNEXPECTED;
801 };
802         
803 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
804 {
805     int i_vlc = _p_instance->getVLCObject();
806     if( i_vlc )
807     {
808         *count = VLC_PlaylistNumberOfItems(i_vlc);
809         return NOERROR;
810     }
811     *count = 0;
812     return E_UNEXPECTED;
813 };
814         
815 STDMETHODIMP VLCControl::playlistNext(void)
816 {
817     int i_vlc = _p_instance->getVLCObject();
818     if( i_vlc )
819     {
820         VLC_PlaylistNext(i_vlc);
821         return NOERROR;
822     }
823     return E_UNEXPECTED;
824 };
825         
826 STDMETHODIMP VLCControl::playlistPrev(void)
827 {
828     int i_vlc = _p_instance->getVLCObject();
829     if( i_vlc )
830     {
831         VLC_PlaylistPrev(i_vlc);
832         return NOERROR;
833     }
834     return E_UNEXPECTED;
835 };
836         
837 STDMETHODIMP VLCControl::playlistClear(void)
838 {
839     int i_vlc = _p_instance->getVLCObject();
840     if( i_vlc )
841     {
842         VLC_PlaylistClear(i_vlc);
843         return NOERROR;
844     }
845     return E_UNEXPECTED;
846 };
847         
848 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
849 {
850     if( NULL == version )
851         return E_POINTER;
852
853     const char *versionStr = VLC_Version();
854     if( NULL != versionStr )
855     {
856         *version = BSTRFromCStr(_p_instance->getCodePage(), versionStr);
857         
858         return NULL == *version ? E_OUTOFMEMORY : NOERROR;
859     }
860     *version = NULL;
861     return E_FAIL;
862 };
863