Removes trailing spaces. Removes tabs.
[vlc.git] / 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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         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 NOERROR;
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;
127     HRESULT result = _p_instance->getVLCObject(&i_vlc);
128     if( SUCCEEDED(result) )
129     {
130         VLC_Play(i_vlc);
131         _p_instance->fireOnPlayEvent();
132     }
133     return result;
134 };
135
136 STDMETHODIMP VLCControl::pause(void)
137 {
138     int i_vlc;
139     HRESULT result = _p_instance->getVLCObject(&i_vlc);
140     if( SUCCEEDED(result) )
141     {
142         VLC_Pause(i_vlc);
143         _p_instance->fireOnPauseEvent();
144     }
145     return result;
146 };
147
148 STDMETHODIMP VLCControl::stop(void)
149 {
150     int i_vlc;
151     HRESULT result = _p_instance->getVLCObject(&i_vlc);
152     if( SUCCEEDED(result) )
153     {
154         VLC_Stop(i_vlc);
155         _p_instance->fireOnStopEvent();
156     }
157     return result;
158 };
159
160 STDMETHODIMP VLCControl::get_Playing(VARIANT_BOOL *isPlaying)
161 {
162     if( NULL == isPlaying )
163         return E_POINTER;
164
165     HRESULT result = NOERROR;
166     if( _p_instance->isRunning() )
167     {
168         int i_vlc;
169         result = _p_instance->getVLCObject(&i_vlc);
170         if( SUCCEEDED(result) )
171         {
172             *isPlaying = VLC_IsPlaying(i_vlc) ? VARIANT_TRUE : VARIANT_FALSE;
173             return NOERROR;
174         }
175     }
176     *isPlaying = VARIANT_FALSE;
177     return result;
178 };
179
180 STDMETHODIMP VLCControl::get_Position(float *position)
181 {
182     if( NULL == position )
183         return E_POINTER;
184
185     HRESULT result = E_UNEXPECTED;
186     if( _p_instance->isRunning() )
187     {
188         int i_vlc;
189         result = _p_instance->getVLCObject(&i_vlc);
190         if( SUCCEEDED(result) )
191         {
192             *position = VLC_PositionGet(i_vlc);
193             return NOERROR;
194         }
195     }
196     *position = 0.0f;
197     return result;
198 };
199
200 STDMETHODIMP VLCControl::put_Position(float position)
201 {
202     HRESULT result = E_UNEXPECTED;
203     if( _p_instance->isRunning() )
204     {
205         int i_vlc;
206         result = _p_instance->getVLCObject(&i_vlc);
207         if( SUCCEEDED(result) )
208         {
209             VLC_PositionSet(i_vlc, position);
210         }
211     }
212     return result;
213 };
214
215 STDMETHODIMP VLCControl::get_Time(int *seconds)
216 {
217     if( NULL == seconds )
218         return E_POINTER;
219
220     HRESULT result = NOERROR;
221     if( _p_instance->isRunning() )
222     {
223         int i_vlc;
224         result = _p_instance->getVLCObject(&i_vlc);
225         if( SUCCEEDED(result) )
226         {
227             *seconds = VLC_TimeGet(i_vlc);
228         }
229     }
230     else
231         *seconds = _p_instance->getTime();
232
233     return result;
234 };
235
236 STDMETHODIMP VLCControl::put_Time(int seconds)
237 {
238     _p_instance->setTime(seconds);
239
240     return NOERROR;
241 };
242
243 STDMETHODIMP VLCControl::shuttle(int seconds)
244 {
245     HRESULT result = E_UNEXPECTED;
246     if( _p_instance->isRunning() )
247     {
248         int i_vlc;
249         result = _p_instance->getVLCObject(&i_vlc);
250         if( SUCCEEDED(result) )
251         {
252             VLC_TimeSet(i_vlc, seconds, VLC_TRUE);
253         }
254     }
255     return result;
256 };
257
258 STDMETHODIMP VLCControl::fullscreen(void)
259 {
260     HRESULT result = E_UNEXPECTED;
261     if( _p_instance->isRunning() )
262     {
263         int i_vlc;
264         result = _p_instance->getVLCObject(&i_vlc);
265         if( SUCCEEDED(result) )
266         {
267             VLC_FullScreen(i_vlc);
268         }
269     }
270     return result;
271 };
272
273 STDMETHODIMP VLCControl::get_Length(int *seconds)
274 {
275     if( NULL == seconds )
276         return E_POINTER;
277
278     HRESULT result = NOERROR;
279     if( _p_instance->isRunning() )
280     {
281         int i_vlc;
282         result = _p_instance->getVLCObject(&i_vlc);
283         if( SUCCEEDED(result) )
284         {
285             *seconds = VLC_LengthGet(i_vlc);
286             return NOERROR;
287         }
288     }
289     *seconds = 0;
290     return result;
291 };
292
293 STDMETHODIMP VLCControl::playFaster(void)
294 {
295     HRESULT result = E_UNEXPECTED;
296     if( _p_instance->isRunning() )
297     {
298         int i_vlc;
299         result = _p_instance->getVLCObject(&i_vlc);
300         if( SUCCEEDED(result) )
301         {
302             VLC_SpeedFaster(i_vlc);
303         }
304     }
305     return result;
306 };
307
308 STDMETHODIMP VLCControl::playSlower(void)
309 {
310     HRESULT result = E_UNEXPECTED;
311     if( _p_instance->isRunning() )
312     {
313         int i_vlc;
314         result = _p_instance->getVLCObject(&i_vlc);
315         if( SUCCEEDED(result) )
316         {
317             VLC_SpeedSlower(i_vlc);
318         }
319     }
320     return result;
321 };
322
323 STDMETHODIMP VLCControl::get_Volume(int *volume)
324 {
325     if( NULL == volume )
326         return E_POINTER;
327
328     *volume  = _p_instance->getVolume();
329     return NOERROR;
330 };
331
332 STDMETHODIMP VLCControl::put_Volume(int volume)
333 {
334     _p_instance->setVolume(volume);
335     return NOERROR;
336 };
337
338 STDMETHODIMP VLCControl::toggleMute(void)
339 {
340     int i_vlc;
341     HRESULT result = _p_instance->getVLCObject(&i_vlc);
342     if( SUCCEEDED(result) )
343     {
344         VLC_VolumeMute(i_vlc);
345     }
346     return result;
347 };
348
349 STDMETHODIMP VLCControl::setVariable(BSTR name, VARIANT value)
350 {
351     if( 0 == SysStringLen(name) )
352         return E_INVALIDARG;
353
354     int i_vlc;
355     HRESULT hr = _p_instance->getVLCObject(&i_vlc);
356     if( SUCCEEDED(hr) )
357     {
358         int codePage = _p_instance->getCodePage();
359         char *psz_varname = CStrFromBSTR(codePage, name);
360         if( NULL == psz_varname )
361             return E_OUTOFMEMORY;
362
363         int i_type;
364         vlc_value_t val;
365
366         if( VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type) )
367         {
368             VARIANT arg;
369             VariantInit(&arg);
370
371             switch( i_type )
372             {
373                 case VLC_VAR_BOOL:
374                     hr = VariantChangeType(&arg, &value, 0, VT_BOOL);
375                     if( SUCCEEDED(hr) )
376                         val.b_bool = (VARIANT_TRUE == V_BOOL(&arg)) ? VLC_TRUE : VLC_FALSE;
377                     break;
378
379                 case VLC_VAR_INTEGER:
380                 case VLC_VAR_HOTKEY:
381                     hr = VariantChangeType(&arg, &value, 0, VT_I4);
382                     if( SUCCEEDED(hr) )
383                         val.i_int = V_I4(&arg);
384                     break;
385
386                 case VLC_VAR_FLOAT:
387                     hr = VariantChangeType(&arg, &value, 0, VT_R4);
388                     if( SUCCEEDED(hr) )
389                         val.f_float = V_R4(&arg);
390                     break;
391
392                 case VLC_VAR_STRING:
393                 case VLC_VAR_MODULE:
394                 case VLC_VAR_FILE:
395                 case VLC_VAR_DIRECTORY:
396                 case VLC_VAR_VARIABLE:
397                     hr = VariantChangeType(&arg, &value, 0, VT_BSTR);
398                     if( SUCCEEDED(hr) )
399                     {
400                         i_type = VLC_VAR_STRING;
401                         val.psz_string = CStrFromBSTR(codePage, V_BSTR(&arg));
402                         VariantClear(&arg);
403                     }
404                     break;
405
406                 case VLC_VAR_TIME:
407                     // use a double value to represent time (base is expressed in seconds)
408                     hr = VariantChangeType(&arg, &value, 0, VT_R8);
409                     if( SUCCEEDED(hr) )
410                         val.i_time = (signed __int64)(V_R8(&arg)*1000000.0);
411                     break;
412
413                 default:
414                     hr = DISP_E_TYPEMISMATCH;
415             }
416         }
417         else {
418             // no defined type, use type in VARIANT
419             hr = NO_ERROR;
420             switch( V_VT(&value) )
421             {
422                 case VT_BOOL:
423                     val.b_bool = (VARIANT_TRUE == V_BOOL(&value)) ? VLC_TRUE : VLC_FALSE;
424                     i_type = VLC_VAR_BOOL;
425                     break;
426                 case VT_I4:
427                     val.i_int = V_I4(&value);
428                     i_type = VLC_VAR_INTEGER;
429                     break;
430                 case VT_R4:
431                     val.f_float = V_R4(&value);
432                     i_type = VLC_VAR_FLOAT;
433                     break;
434                 case VT_BSTR:
435                     val.psz_string = CStrFromBSTR(codePage, V_BSTR(&value));
436                     i_type = VLC_VAR_STRING;
437                     break;
438                 case VT_R8:
439                     // use a double value to represent time (base is expressed in seconds)
440                     val.i_time = (signed __int64)(V_R8(&value)*1000000.0);
441                     i_type = VLC_VAR_TIME;
442                     break;
443                 default:
444                     hr = DISP_E_TYPEMISMATCH;
445             }
446         }
447         if( SUCCEEDED(hr) )
448         {
449             hr = (VLC_SUCCESS == VLC_VariableSet(i_vlc, psz_varname, val)) ? NOERROR : E_FAIL;
450
451             if( (VLC_VAR_STRING == i_type) && (NULL != val.psz_string) )
452                 CoTaskMemFree(val.psz_string);
453         }
454         CoTaskMemFree(psz_varname);
455     }
456     return hr;
457 };
458
459 STDMETHODIMP VLCControl::getVariable( BSTR name, VARIANT *value)
460 {
461     if( NULL == value )
462         return E_POINTER;
463
464     VariantInit(value);
465
466     if( 0 == SysStringLen(name) )
467         return E_INVALIDARG;
468
469     int i_vlc;
470     HRESULT hr = _p_instance->getVLCObject(&i_vlc);
471     if( SUCCEEDED(hr) )
472     {
473         UINT codePage = _p_instance->getCodePage();
474         char *psz_varname = CStrFromBSTR(codePage, name);
475         if( NULL == psz_varname )
476             return E_OUTOFMEMORY;
477
478         hr = E_INVALIDARG;
479
480         vlc_value_t val;
481         int i_type;
482
483         if( (VLC_SUCCESS == VLC_VariableGet(i_vlc, psz_varname, &val))
484          && (VLC_SUCCESS == VLC_VariableType(i_vlc, psz_varname, &i_type)) )
485         {
486             hr = NOERROR;
487             switch( i_type )
488             {
489                 case VLC_VAR_BOOL:
490                     V_VT(value) = VT_BOOL;
491                     V_BOOL(value) = val.b_bool ? VARIANT_TRUE : VARIANT_FALSE;
492                     break;
493
494                 case VLC_VAR_INTEGER:
495                 case VLC_VAR_HOTKEY:
496                     V_VT(value) = VT_I4;
497                     V_I4(value) = val.i_int;
498                     break;
499
500                 case VLC_VAR_FLOAT:
501                     V_VT(value) = VT_R4;
502                     V_R4(value) = val.f_float;
503                     break;
504
505                 case VLC_VAR_STRING:
506                 case VLC_VAR_MODULE:
507                 case VLC_VAR_FILE:
508                 case VLC_VAR_DIRECTORY:
509                 case VLC_VAR_VARIABLE:
510                     V_VT(value) = VT_BSTR;
511                     V_BSTR(value) = BSTRFromCStr(codePage, val.psz_string);
512                     if( NULL != val.psz_string)
513                         free(val.psz_string);
514                     break;
515
516                 case VLC_VAR_TIME:
517                     // use a double value to represent time (base is expressed in seconds)
518                     V_VT(value) = VT_R8;
519                     V_R8(value) = ((double)val.i_time)/1000000.0;
520                     break;
521
522                 default:
523                     hr = DISP_E_TYPEMISMATCH;
524             }
525         }
526         CoTaskMemFree(psz_varname);
527         return hr;
528     }
529     return hr;
530 };
531
532 void VLCControl::FreeTargetOptions(char **cOptions, int cOptionCount)
533 {
534     // clean up
535     if( NULL != cOptions )
536     {
537         for( int pos=0; pos<cOptionCount; ++pos )
538         {
539             char *cOption = cOptions[pos];
540             if( NULL != cOption )
541                 CoTaskMemFree(cOption);
542             else
543                 break;
544         }
545         CoTaskMemFree(cOptions);
546     }
547 };
548
549 static HRESULT parseStringOptions(int codePage, BSTR bstr, char*** cOptions, int *cOptionCount)
550 {
551     HRESULT hr = E_INVALIDARG;
552     if( SysStringLen(bstr) > 0 )
553     {
554         hr = E_OUTOFMEMORY;
555         char *s = CStrFromBSTR(codePage, bstr);
556         char *val = s;
557         if( val )
558         {
559             long capacity = 16;
560             char **options = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
561             if( options )
562             {
563                 int nOptions = 0;
564
565                 char *end = val + strlen(val);
566                 while( val < end )
567                 {
568                     // skip leading blanks
569                     while( (val < end)
570                         && ((*val == ' ' ) || (*val == '\t')) )
571                         ++val;
572
573                     char *start = val;
574                     // skip till we get a blank character
575                     while( (val < end)
576                         && (*val != ' ' )
577                         && (*val != '\t') )
578                     {
579                         char c = *(val++);
580                         if( ('\'' == c) || ('"' == c) )
581                         {
582                             // skip till end of string
583                             while( (val < end) && (*(val++) != c ) );
584                         }
585                     }
586
587                     if( val > start )
588                     {
589                         if( nOptions == capacity )
590                         {
591                             capacity += 16;
592                             char **moreOptions = (char **)CoTaskMemRealloc(options, capacity*sizeof(char*));
593                             if( ! moreOptions )
594                             {
595                                 /* failed to allocate more memory */
596                                 CoTaskMemFree(s);
597                                 /* return what we got so far */
598                                 *cOptionCount = nOptions;
599                                 *cOptions = options;
600                                 return NOERROR;
601                             }
602                             options = moreOptions;
603                         }
604                         *(val++) = '\0';
605                         options[nOptions] = (char *)CoTaskMemAlloc(val-start);
606                         if( options[nOptions] )
607                         {
608                             memcpy(options[nOptions], start, val-start);
609                             ++nOptions;
610                         }
611                         else
612                         {
613                             /* failed to allocate memory */
614                             CoTaskMemFree(s);
615                             /* return what we got so far */
616                             *cOptionCount = nOptions;
617                             *cOptions = options;
618                             return NOERROR;
619                         }
620                     }
621                     else
622                         // must be end of string
623                         break;
624                 }
625                 *cOptionCount = nOptions;
626                 *cOptions = options;
627                 hr = NOERROR;
628             }
629             CoTaskMemFree(s);
630         }
631     }
632     return hr;
633 }
634
635 HRESULT VLCControl::CreateTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
636 {
637     HRESULT hr = E_INVALIDARG;
638     if( VT_ERROR == V_VT(options) )
639     {
640         if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
641         {
642             // optional parameter not set
643             *cOptions = NULL;
644             *cOptionCount = 0;
645             return NOERROR;
646         }
647     }
648     else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
649     {
650         // null parameter
651         *cOptions = NULL;
652         *cOptionCount = 0;
653         return NOERROR;
654     }
655     else if( VT_DISPATCH == V_VT(options) )
656     {
657         // if object is a collection, retrieve enumerator
658         VARIANT colEnum;
659         V_VT(&colEnum) = VT_UNKNOWN;
660         hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
661         if( SUCCEEDED(hr) )
662         {
663             IEnumVARIANT *enumVar;
664             hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
665             if( SUCCEEDED(hr) )
666             {
667                 long pos = 0;
668                 long capacity = 16;
669                 VARIANT option;
670
671                 *cOptions = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
672                 if( NULL != *cOptions )
673                 {
674                     ZeroMemory(*cOptions, sizeof(char *)*capacity);
675                     while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
676                     {
677                         if( VT_BSTR == V_VT(&option) )
678                         {
679                             char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
680                             (*cOptions)[pos] = cOption;
681                             if( NULL != cOption )
682                             {
683                                 ++pos;
684                                 if( pos == capacity )
685                                 {
686                                     char **moreOptions = (char **)CoTaskMemRealloc(*cOptions, (capacity+16)*sizeof(char *));
687                                     if( NULL != moreOptions )
688                                     {
689                                         ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
690                                         capacity += 16;
691                                         *cOptions = moreOptions;
692                                     }
693                                     else
694                                         hr = E_OUTOFMEMORY;
695                                 }
696                             }
697                             else
698                                 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
699                                     E_OUTOFMEMORY : E_INVALIDARG;
700                         }
701                         else
702                             hr = E_INVALIDARG;
703
704                         VariantClear(&option);
705                     }
706                     *cOptionCount = pos;
707                     if( FAILED(hr) )
708                     {
709                         // free already processed elements
710                         FreeTargetOptions(*cOptions, *cOptionCount);
711                     }
712                 }
713                 else
714                     hr = E_OUTOFMEMORY;
715
716                 enumVar->Release();
717             }
718         }
719         else
720         {
721             // coerce object into a string and parse it
722             VARIANT v_name;
723             VariantInit(&v_name);
724             hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
725             if( SUCCEEDED(hr) )
726             {
727                 hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
728                 VariantClear(&v_name);
729             }
730         }
731     }
732     else if( V_ISARRAY(options) )
733     {
734         // array parameter
735         SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
736
737         if( SafeArrayGetDim(array) != 1 )
738             return E_INVALIDARG;
739
740         long lBound = 0;
741         long uBound = 0;
742         SafeArrayGetLBound(array, 1, &lBound);
743         SafeArrayGetUBound(array, 1, &uBound);
744
745         // have we got any options
746         if( uBound >= lBound )
747         {
748             VARTYPE vType;
749             hr = SafeArrayGetVartype(array, &vType);
750             if( FAILED(hr) )
751                 return hr;
752
753             long pos;
754
755             // marshall options into an array of C strings
756             if( VT_VARIANT == vType )
757             {
758                 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
759                 if( NULL == *cOptions )
760                     return E_OUTOFMEMORY;
761
762                 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
763                 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
764                 {
765                     VARIANT option;
766                     hr = SafeArrayGetElement(array, &pos, &option);
767                     if( SUCCEEDED(hr) )
768                     {
769                         if( VT_BSTR == V_VT(&option) )
770                         {
771                             char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
772                             (*cOptions)[pos-lBound] = cOption;
773                             if( NULL == cOption )
774                                 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
775                                     E_OUTOFMEMORY : E_INVALIDARG;
776                         }
777                         else
778                             hr = E_INVALIDARG;
779                         VariantClear(&option);
780                     }
781                 }
782             }
783             else if( VT_BSTR == vType )
784             {
785                 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
786                 if( NULL == *cOptions )
787                     return E_OUTOFMEMORY;
788
789                 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
790                 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
791                 {
792                     BSTR option;
793                     hr = SafeArrayGetElement(array, &pos, &option);
794                     if( SUCCEEDED(hr) )
795                     {
796                         char *cOption = CStrFromBSTR(codePage, option);
797
798                         (*cOptions)[pos-lBound] = cOption;
799                         if( NULL == cOption )
800                             hr = ( SysStringLen(option) > 0 ) ?
801                                 E_OUTOFMEMORY : E_INVALIDARG;
802                         SysFreeString(option);
803                     }
804                 }
805             }
806             else
807             {
808                 // unsupported type
809                 return E_INVALIDARG;
810             }
811
812             *cOptionCount = pos-lBound;
813             if( FAILED(hr) )
814             {
815                 // free already processed elements
816                 FreeTargetOptions(*cOptions, *cOptionCount);
817             }
818         }
819         else
820         {
821             // empty array
822             *cOptions = NULL;
823             *cOptionCount = 0;
824             return NOERROR;
825         }
826     }
827     else if( VT_UNKNOWN == V_VT(options) )
828     {
829         // coerce object into a string and parse it
830         VARIANT v_name;
831         VariantInit(&v_name);
832         hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
833         if( SUCCEEDED(hr) )
834         {
835             hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
836             VariantClear(&v_name);
837         }
838     }
839     else if( VT_BSTR == V_VT(options) )
840     {
841         hr = parseStringOptions(codePage, V_BSTR(options), cOptions, cOptionCount);
842     }
843     return hr;
844 };
845
846 /*
847 ** use VARIANT rather than a SAFEARRAY as argument type
848 ** for compatibility with some scripting language (JScript)
849 */
850
851 STDMETHODIMP VLCControl::addTarget( BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
852 {
853     if( 0 == SysStringLen(uri) )
854         return E_INVALIDARG;
855
856     int i_vlc;
857     HRESULT hr = _p_instance->getVLCObject(&i_vlc);
858     if( SUCCEEDED(hr) )
859     {
860         char *cUri = CStrFromBSTR(CP_UTF8, uri);
861         if( NULL == cUri )
862             return E_OUTOFMEMORY;
863
864         int cOptionsCount;
865         char **cOptions;
866
867         if( FAILED(CreateTargetOptions(CP_UTF8, &options, &cOptions, &cOptionsCount)) )
868             return E_INVALIDARG;
869
870         if( VLC_SUCCESS <= VLC_AddTarget(i_vlc, cUri, (const char **)cOptions, cOptionsCount, mode, position) )
871         {
872             hr = NOERROR;
873             if( mode & PLAYLIST_GO )
874                 _p_instance->fireOnPlayEvent();
875         }
876         else
877         {
878             hr = E_FAIL;
879             if( mode & PLAYLIST_GO )
880                 _p_instance->fireOnStopEvent();
881         }
882
883         FreeTargetOptions(cOptions, cOptionsCount);
884         CoTaskMemFree(cUri);
885     }
886     return hr;
887 };
888
889 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
890 {
891     if( NULL == index )
892         return E_POINTER;
893
894     int i_vlc;
895     HRESULT result = _p_instance->getVLCObject(&i_vlc);
896     if( SUCCEEDED(result) )
897     {
898         *index = VLC_PlaylistIndex(i_vlc);
899         return NOERROR;
900     }
901     *index = 0;
902     return result;
903 };
904
905 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
906 {
907     int i_vlc;
908     HRESULT result = _p_instance->getVLCObject(&i_vlc);
909     if( SUCCEEDED(result) )
910     {
911         *count = VLC_PlaylistNumberOfItems(i_vlc);
912         return NOERROR;
913     }
914     *count = 0;
915     return result;
916 };
917
918 STDMETHODIMP VLCControl::playlistNext(void)
919 {
920     int i_vlc;
921     HRESULT result = _p_instance->getVLCObject(&i_vlc);
922     if( SUCCEEDED(result) )
923     {
924         VLC_PlaylistNext(i_vlc);
925         return NOERROR;
926     }
927     return result;
928 };
929
930 STDMETHODIMP VLCControl::playlistPrev(void)
931 {
932     int i_vlc;
933     HRESULT result = _p_instance->getVLCObject(&i_vlc);
934     if( SUCCEEDED(result) )
935     {
936         VLC_PlaylistPrev(i_vlc);
937         return NOERROR;
938     }
939     return result;
940 };
941
942 STDMETHODIMP VLCControl::playlistClear(void)
943 {
944     int i_vlc;
945     HRESULT result = _p_instance->getVLCObject(&i_vlc);
946     if( SUCCEEDED(result) )
947     {
948         VLC_PlaylistClear(i_vlc);
949         return NOERROR;
950     }
951     return result;
952 };
953
954 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
955 {
956     if( NULL == version )
957         return E_POINTER;
958
959     const char *versionStr = VLC_Version();
960     if( NULL != versionStr )
961     {
962         *version = BSTRFromCStr(CP_UTF8, versionStr);
963  
964         return NULL == *version ? E_OUTOFMEMORY : NOERROR;
965     }
966     *version = NULL;
967     return E_FAIL;
968 };
969
970 STDMETHODIMP VLCControl::get_MRL(BSTR *mrl)
971 {
972     if( NULL == mrl )
973         return E_POINTER;
974
975     *mrl = SysAllocStringLen(_p_instance->getMRL(),
976                 SysStringLen(_p_instance->getMRL()));
977     return NOERROR;
978 };
979
980 STDMETHODIMP VLCControl::put_MRL(BSTR mrl)
981 {
982     _p_instance->setMRL(mrl);
983
984     return S_OK;
985 };
986
987 STDMETHODIMP VLCControl::get_AutoPlay(VARIANT_BOOL *autoplay)
988 {
989     if( NULL == autoplay )
990         return E_POINTER;
991
992     *autoplay = _p_instance->getAutoPlay() ? VARIANT_TRUE: VARIANT_FALSE;
993     return S_OK;
994 };
995
996 STDMETHODIMP VLCControl::put_AutoPlay(VARIANT_BOOL autoplay)
997 {
998     _p_instance->setAutoPlay((VARIANT_FALSE != autoplay) ? TRUE: FALSE);
999     return S_OK;
1000 };
1001
1002 STDMETHODIMP VLCControl::get_AutoLoop(VARIANT_BOOL *autoloop)
1003 {
1004     if( NULL == autoloop )
1005         return E_POINTER;
1006
1007     *autoloop = _p_instance->getAutoLoop() ? VARIANT_TRUE: VARIANT_FALSE;
1008     return S_OK;
1009 };
1010
1011 STDMETHODIMP VLCControl::put_AutoLoop(VARIANT_BOOL autoloop)
1012 {
1013     _p_instance->setAutoLoop((VARIANT_FALSE != autoloop) ? TRUE: FALSE);
1014     return S_OK;
1015 };