]> git.sesse.net Git - vlc/blob - projects/activex/vlccontrol.cpp
Add comment about getVariable() and setVariable() JS function: they are removed becau...
[vlc] / projects / 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  *          Jean-Paul Saman <jpsaman@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "plugin.h"
25 #include "vlccontrol.h"
26
27 #include "utils.h"
28
29 using namespace std;
30
31 VLCControl::~VLCControl()
32 {
33     if( _p_typeinfo )
34         _p_typeinfo->Release();
35 };
36
37 HRESULT VLCControl::getTypeInfo(void)
38 {
39     HRESULT hr = NOERROR;
40     if( NULL == _p_typeinfo )
41     {
42         ITypeLib *p_typelib;
43
44         hr = _p_instance->getTypeLib(LOCALE_USER_DEFAULT, &p_typelib);
45         if( SUCCEEDED(hr) )
46         {
47             hr = p_typelib->GetTypeInfoOfGuid(IID_IVLCControl, &_p_typeinfo);
48             if( FAILED(hr) )
49             {
50                 _p_typeinfo = NULL;
51             }
52             p_typelib->Release();
53         }
54     }
55     return hr;
56 };
57
58 STDMETHODIMP VLCControl::GetTypeInfoCount(UINT* pctInfo)
59 {
60     if( NULL == pctInfo )
61         return E_INVALIDARG;
62
63     if( SUCCEEDED(getTypeInfo()) )
64         *pctInfo = 1;
65     else
66         *pctInfo = 0;
67
68     return NOERROR;
69 };
70
71 STDMETHODIMP VLCControl::GetTypeInfo(UINT iTInfo, LCID lcid, LPTYPEINFO* ppTInfo)
72 {
73     if( NULL == ppTInfo )
74         return E_INVALIDARG;
75
76     if( SUCCEEDED(getTypeInfo()) )
77     {
78         _p_typeinfo->AddRef();
79         *ppTInfo = _p_typeinfo;
80         return NOERROR;
81     }
82     *ppTInfo = NULL;
83     return E_NOTIMPL;
84 };
85
86 STDMETHODIMP VLCControl::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames,
87         UINT cNames, LCID lcid, DISPID* rgDispID)
88 {
89     if( SUCCEEDED(getTypeInfo()) )
90     {
91         return DispGetIDsOfNames(_p_typeinfo, rgszNames, cNames, rgDispID);
92     }
93     return E_NOTIMPL;
94 };
95
96 STDMETHODIMP VLCControl::Invoke(DISPID dispIdMember, REFIID riid,
97         LCID lcid, WORD wFlags, DISPPARAMS* pDispParams,
98         VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
99 {
100     if( SUCCEEDED(getTypeInfo()) )
101     {
102         return DispInvoke(this, _p_typeinfo, dispIdMember, wFlags, pDispParams,
103                 pVarResult, pExcepInfo, puArgErr);
104     }
105     return E_NOTIMPL;
106 };
107
108 STDMETHODIMP VLCControl::get_Visible(VARIANT_BOOL *isVisible)
109 {
110     if( NULL == isVisible )
111         return E_POINTER;
112
113     *isVisible = _p_instance->getVisible() ? VARIANT_TRUE : VARIANT_FALSE;
114
115     return NOERROR;
116 };
117
118 STDMETHODIMP VLCControl::put_Visible(VARIANT_BOOL isVisible)
119 {
120     _p_instance->setVisible(isVisible != VARIANT_FALSE);
121
122     return NOERROR;
123 };
124
125 STDMETHODIMP VLCControl::play(void)
126 {
127     libvlc_instance_t* p_libvlc;
128     HRESULT result = _p_instance->getVLC(&p_libvlc);
129     if( SUCCEEDED(result) )
130     {
131         libvlc_exception_t ex;
132         libvlc_exception_init(&ex);
133
134         libvlc_playlist_play(p_libvlc, -1, 0, NULL, &ex);
135         if( libvlc_exception_raised(&ex) )
136         {
137             _p_instance->setErrorInfo(IID_IVLCControl,
138                 libvlc_exception_get_message(&ex));
139             libvlc_exception_clear(&ex);
140             return E_FAIL;
141         }
142         _p_instance->fireOnPlayEvent();
143         return NOERROR;
144     }
145     return result;
146 };
147
148 STDMETHODIMP VLCControl::pause(void)
149 {
150     libvlc_instance_t* p_libvlc;
151     HRESULT result = _p_instance->getVLC(&p_libvlc);
152     if( SUCCEEDED(result) )
153     {
154         libvlc_exception_t ex;
155         libvlc_exception_init(&ex);
156
157         libvlc_playlist_pause(p_libvlc, &ex);
158         if( libvlc_exception_raised(&ex) )
159         {
160             _p_instance->setErrorInfo(IID_IVLCControl,
161                 libvlc_exception_get_message(&ex));
162             libvlc_exception_clear(&ex);
163             return E_FAIL;
164         }
165         _p_instance->fireOnPauseEvent();
166         return NOERROR;
167     }
168     return result;
169 };
170
171 STDMETHODIMP VLCControl::stop(void)
172 {
173     libvlc_instance_t* p_libvlc;
174     HRESULT result = _p_instance->getVLC(&p_libvlc);
175     if( SUCCEEDED(result) )
176     {
177         libvlc_exception_t ex;
178         libvlc_exception_init(&ex);
179
180         libvlc_playlist_stop(p_libvlc, &ex);
181         if( libvlc_exception_raised(&ex) )
182         {
183             _p_instance->setErrorInfo(IID_IVLCControl,
184                 libvlc_exception_get_message(&ex));
185             libvlc_exception_clear(&ex);
186             return E_FAIL;
187         }
188         return NOERROR;
189     }
190     _p_instance->fireOnStopEvent();
191     return result;
192 };
193
194 STDMETHODIMP VLCControl::get_Playing(VARIANT_BOOL *isPlaying)
195 {
196     if( NULL == isPlaying )
197         return E_POINTER;
198
199     HRESULT result = NOERROR;
200     if( _p_instance->isRunning() )
201     {
202         libvlc_instance_t *p_libvlc;
203         result = _p_instance->getVLC(&p_libvlc);
204         if( SUCCEEDED(result) )
205         {
206             if( libvlc_playlist_isplaying(p_libvlc, NULL) )
207                 *isPlaying = VARIANT_TRUE;
208             else
209                 *isPlaying = VARIANT_FALSE;
210             return NOERROR;
211         }
212     }
213     *isPlaying = VARIANT_FALSE;
214     return result;
215 };
216
217 STDMETHODIMP VLCControl::get_Position(float *position)
218 {
219     if( NULL == position )
220         return E_POINTER;
221     *position = 0.0f;
222
223     libvlc_instance_t* p_libvlc;
224     HRESULT result = E_UNEXPECTED;
225     result = _p_instance->getVLC(&p_libvlc);
226     if( SUCCEEDED(result) )
227     {
228         libvlc_exception_t ex;
229         libvlc_exception_init(&ex);
230
231         libvlc_media_player_t *p_md;
232         p_md = libvlc_playlist_get_media_player(p_libvlc, &ex);
233         if( !libvlc_exception_raised(&ex) )
234         {
235             *position = libvlc_media_player_get_position(p_md, &ex);
236             libvlc_media_player_release(p_md);
237             if( ! libvlc_exception_raised(&ex) )
238             {
239                 return NOERROR;
240             }
241         }
242         _p_instance->setErrorInfo(IID_IVLCControl,
243                      libvlc_exception_get_message(&ex));
244         libvlc_exception_clear(&ex);
245         return E_FAIL;
246     }
247     return result;
248 };
249
250 STDMETHODIMP VLCControl::put_Position(float position)
251 {
252     HRESULT result = E_UNEXPECTED;
253     libvlc_instance_t* p_libvlc;
254     result = _p_instance->getVLC(&p_libvlc);
255     if( SUCCEEDED(result) )
256     {
257         libvlc_exception_t ex;
258         libvlc_exception_init(&ex);
259
260         libvlc_media_player_t *p_md;
261         p_md = libvlc_playlist_get_media_player(p_libvlc, &ex);
262         if( ! libvlc_exception_raised(&ex) )
263         {
264             libvlc_media_player_set_position(p_md, position, &ex);
265             libvlc_media_player_release(p_md);
266             if( ! libvlc_exception_raised(&ex) )
267             {
268                 return NOERROR;
269             }
270         }
271         _p_instance->setErrorInfo(IID_IVLCControl,
272                      libvlc_exception_get_message(&ex));
273         libvlc_exception_clear(&ex);
274         return E_FAIL;
275     }
276     return result;
277 };
278
279 STDMETHODIMP VLCControl::get_Time(int *seconds)
280 {
281     if( NULL == seconds )
282         return E_POINTER;
283
284     *seconds = 0;
285     libvlc_instance_t* p_libvlc;
286     HRESULT result = _p_instance->getVLC(&p_libvlc);
287     if( SUCCEEDED(result) )
288     {
289         libvlc_exception_t ex;
290         libvlc_exception_init(&ex);
291
292         libvlc_media_player_t *p_md;
293         p_md = libvlc_playlist_get_media_player(p_libvlc, &ex);
294         if( ! libvlc_exception_raised(&ex) )
295         {
296             *seconds = libvlc_media_player_get_time(p_md, &ex);
297             libvlc_media_player_release(p_md);
298             if( ! libvlc_exception_raised(&ex) )
299             {
300                 return NOERROR;
301             }
302         }
303         _p_instance->setErrorInfo(IID_IVLCControl,
304                      libvlc_exception_get_message(&ex));
305         libvlc_exception_clear(&ex);
306         return E_FAIL;
307     }
308     return result;
309 };
310
311 STDMETHODIMP VLCControl::put_Time(int seconds)
312 {
313     /* setTime function of the plugin sets the time. */
314     _p_instance->setTime(seconds);
315     return NOERROR;
316 };
317
318 STDMETHODIMP VLCControl::shuttle(int seconds)
319 {
320     libvlc_instance_t* p_libvlc;
321     HRESULT result = _p_instance->getVLC(&p_libvlc);
322     if( SUCCEEDED(result) )
323     {
324         libvlc_exception_t ex;
325         libvlc_exception_init(&ex);
326
327         libvlc_media_player_t *p_md;
328         p_md = libvlc_playlist_get_media_player(p_libvlc, &ex);
329         if( ! libvlc_exception_raised(&ex) )
330         {
331             if( seconds < 0 ) seconds = 0;
332             libvlc_media_player_set_time(p_md, (int64_t)seconds, &ex);
333             libvlc_media_player_release(p_md);
334             if( ! libvlc_exception_raised(&ex) )
335             {
336                 return NOERROR;
337             }
338         }
339         _p_instance->setErrorInfo(IID_IVLCControl,
340                      libvlc_exception_get_message(&ex));
341         libvlc_exception_clear(&ex);
342         return E_FAIL;
343     }
344     return result;
345
346 };
347
348 STDMETHODIMP VLCControl::fullscreen(void)
349 {
350     HRESULT result = E_UNEXPECTED;
351     if( _p_instance->isRunning() )
352     {
353         libvlc_instance_t *p_libvlc;
354         result = _p_instance->getVLC(&p_libvlc);
355         if( SUCCEEDED(result) )
356         {
357             if( libvlc_playlist_isplaying(p_libvlc, NULL) )
358             {
359                 libvlc_media_player_t *p_md =
360                     libvlc_playlist_get_media_player(p_libvlc, NULL);
361                 if( p_md )
362                 {
363                     libvlc_toggle_fullscreen(p_md, NULL);
364                     libvlc_media_player_release(p_md);
365                 }
366             }
367         }
368     }
369     return result;
370 };
371
372 STDMETHODIMP VLCControl::get_Length(int *seconds)
373 {
374     if( NULL == seconds )
375         return E_POINTER;
376     *seconds = 0;
377
378     libvlc_instance_t* p_libvlc;
379     HRESULT result = _p_instance->getVLC(&p_libvlc);
380     if( SUCCEEDED(result) )
381     {
382         libvlc_exception_t ex;
383         libvlc_exception_init(&ex);
384
385         libvlc_media_player_t *p_md;
386         p_md = libvlc_playlist_get_media_player(p_libvlc, &ex);
387         if( ! libvlc_exception_raised(&ex) )
388         {
389             *seconds = (double)libvlc_media_player_get_length(p_md, &ex);
390             libvlc_media_player_release(p_md);
391             if( ! libvlc_exception_raised(&ex) )
392             {
393                 return NOERROR;
394             }
395         }
396         _p_instance->setErrorInfo(IID_IVLCControl,
397                      libvlc_exception_get_message(&ex));
398         libvlc_exception_clear(&ex);
399         return E_FAIL;
400     }
401     return result;
402
403 };
404
405 STDMETHODIMP VLCControl::playFaster(void)
406 {
407     int32_t rate = 2;
408
409     HRESULT result = E_UNEXPECTED;
410     if( !_p_instance->isRunning() )
411         return result;
412
413     libvlc_instance_t* p_libvlc;
414     result = _p_instance->getVLC(&p_libvlc);
415     if( SUCCEEDED(result) )
416     {
417         libvlc_exception_t ex;
418         libvlc_exception_init(&ex);
419
420         libvlc_media_player_t *p_md;
421         p_md = libvlc_playlist_get_media_player(p_libvlc, &ex);
422         if( ! libvlc_exception_raised(&ex) )
423         {
424             libvlc_media_player_set_rate(p_md, rate, &ex);
425             libvlc_media_player_release(p_md);
426             if( ! libvlc_exception_raised(&ex) )
427             {
428                 return NOERROR;
429             }
430         }
431         _p_instance->setErrorInfo(IID_IVLCControl,
432                      libvlc_exception_get_message(&ex));
433         libvlc_exception_clear(&ex);
434         return E_FAIL;
435     }
436     return result;
437 };
438
439 STDMETHODIMP VLCControl::playSlower(void)
440 {
441     float rate = 0.5;
442
443     HRESULT result = E_UNEXPECTED;
444     if( !_p_instance->isRunning() )
445         return result;
446
447     libvlc_instance_t* p_libvlc;
448     result = _p_instance->getVLC(&p_libvlc);
449     if( SUCCEEDED(result) )
450     {
451         libvlc_exception_t ex;
452         libvlc_exception_init(&ex);
453
454         libvlc_media_player_t *p_md;
455         p_md = libvlc_playlist_get_media_player(p_libvlc, &ex);
456         if( ! libvlc_exception_raised(&ex) )
457         {
458             libvlc_media_player_set_rate(p_md, rate, &ex);
459             libvlc_media_player_release(p_md);
460             if( ! libvlc_exception_raised(&ex) )
461             {
462                 return NOERROR;
463             }
464         }
465         _p_instance->setErrorInfo(IID_IVLCControl,
466                      libvlc_exception_get_message(&ex));
467         libvlc_exception_clear(&ex);
468         return E_FAIL;
469     }
470     return result;
471 };
472
473 STDMETHODIMP VLCControl::get_Volume(int *volume)
474 {
475     if( NULL == volume )
476         return E_POINTER;
477
478     *volume  = _p_instance->getVolume();
479     return NOERROR;
480 };
481
482 STDMETHODIMP VLCControl::put_Volume(int volume)
483 {
484     _p_instance->setVolume(volume);
485     return NOERROR;
486 };
487
488 STDMETHODIMP VLCControl::toggleMute(void)
489 {
490     libvlc_instance_t* p_libvlc;
491     HRESULT result = _p_instance->getVLC(&p_libvlc);
492     if( SUCCEEDED(result) )
493     {
494         libvlc_exception_t ex;
495         libvlc_exception_init(&ex);
496
497         libvlc_audio_toggle_mute(p_libvlc, &ex);
498         if( libvlc_exception_raised(&ex) )
499         {
500             _p_instance->setErrorInfo(IID_IVLCControl,
501                          libvlc_exception_get_message(&ex));
502             libvlc_exception_clear(&ex);
503             return E_FAIL;
504         }
505         return NOERROR;
506     }
507     return result;
508 };
509
510 STDMETHODIMP VLCControl::setVariable(BSTR name, VARIANT value)
511 {
512     /** setVariable() is an unsafe interface because of security
513       implications it has and is thus removed. */
514     return E_INVALIDARG;
515 };
516
517 STDMETHODIMP VLCControl::getVariable(BSTR name, VARIANT *value)
518 {
519     /** getVariable() is an unsafe interface because of security
520       implications it has and is thus removed. */
521     return E_INVALIDARG;
522 };
523
524 void VLCControl::FreeTargetOptions(char **cOptions, int cOptionCount)
525 {
526     // clean up
527     if( NULL != cOptions )
528     {
529         for( int pos=0; pos<cOptionCount; ++pos )
530         {
531             char *cOption = cOptions[pos];
532             if( NULL != cOption )
533                 CoTaskMemFree(cOption);
534             else
535                 break;
536         }
537         CoTaskMemFree(cOptions);
538     }
539 };
540
541 static HRESULT parseStringOptions(int codePage, BSTR bstr, char*** cOptions, int *cOptionCount)
542 {
543     HRESULT hr = E_INVALIDARG;
544     if( SysStringLen(bstr) > 0 )
545     {
546         hr = E_OUTOFMEMORY;
547         char *s = CStrFromBSTR(codePage, bstr);
548         char *val = s;
549         if( val )
550         {
551             long capacity = 16;
552             char **options = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
553             if( options )
554             {
555                 int nOptions = 0;
556
557                 char *end = val + strlen(val);
558                 while( val < end )
559                 {
560                     // skip leading blanks
561                     while( (val < end)
562                         && ((*val == ' ' ) || (*val == '\t')) )
563                         ++val;
564
565                     char *start = val;
566                     // skip till we get a blank character
567                     while( (val < end)
568                         && (*val != ' ' )
569                         && (*val != '\t') )
570                     {
571                         char c = *(val++);
572                         if( ('\'' == c) || ('"' == c) )
573                         {
574                             // skip till end of string
575                             while( (val < end) && (*(val++) != c ) );
576                         }
577                     }
578
579                     if( val > start )
580                     {
581                         if( nOptions == capacity )
582                         {
583                             capacity += 16;
584                             char **moreOptions = (char **)CoTaskMemRealloc(options, capacity*sizeof(char*));
585                             if( ! moreOptions )
586                             {
587                                 /* failed to allocate more memory */
588                                 CoTaskMemFree(s);
589                                 /* return what we got so far */
590                                 *cOptionCount = nOptions;
591                                 *cOptions = options;
592                                 return NOERROR;
593                             }
594                             options = moreOptions;
595                         }
596                         *(val++) = '\0';
597                         options[nOptions] = (char *)CoTaskMemAlloc(val-start);
598                         if( options[nOptions] )
599                         {
600                             memcpy(options[nOptions], start, val-start);
601                             ++nOptions;
602                         }
603                         else
604                         {
605                             /* failed to allocate memory */
606                             CoTaskMemFree(s);
607                             /* return what we got so far */
608                             *cOptionCount = nOptions;
609                             *cOptions = options;
610                             return NOERROR;
611                         }
612                     }
613                     else
614                         // must be end of string
615                         break;
616                 }
617                 *cOptionCount = nOptions;
618                 *cOptions = options;
619                 hr = NOERROR;
620             }
621             CoTaskMemFree(s);
622         }
623     }
624     return hr;
625 }
626
627 HRESULT VLCControl::CreateTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
628 {
629     HRESULT hr = E_INVALIDARG;
630     if( VT_ERROR == V_VT(options) )
631     {
632         if( DISP_E_PARAMNOTFOUND == V_ERROR(options) )
633         {
634             // optional parameter not set
635             *cOptions = NULL;
636             *cOptionCount = 0;
637             return NOERROR;
638         }
639     }
640     else if( (VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)) )
641     {
642         // null parameter
643         *cOptions = NULL;
644         *cOptionCount = 0;
645         return NOERROR;
646     }
647     else if( VT_DISPATCH == V_VT(options) )
648     {
649         // if object is a collection, retrieve enumerator
650         VARIANT colEnum;
651         V_VT(&colEnum) = VT_UNKNOWN;
652         hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
653         if( SUCCEEDED(hr) )
654         {
655             IEnumVARIANT *enumVar;
656             hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *)&enumVar);
657             if( SUCCEEDED(hr) )
658             {
659                 long pos = 0;
660                 long capacity = 16;
661                 VARIANT option;
662
663                 *cOptions = (char **)CoTaskMemAlloc(capacity*sizeof(char *));
664                 if( NULL != *cOptions )
665                 {
666                     ZeroMemory(*cOptions, sizeof(char *)*capacity);
667                     while( SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)) )
668                     {
669                         if( VT_BSTR == V_VT(&option) )
670                         {
671                             char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
672                             (*cOptions)[pos] = cOption;
673                             if( NULL != cOption )
674                             {
675                                 ++pos;
676                                 if( pos == capacity )
677                                 {
678                                     char **moreOptions = (char **)CoTaskMemRealloc(*cOptions, (capacity+16)*sizeof(char *));
679                                     if( NULL != moreOptions )
680                                     {
681                                         ZeroMemory(moreOptions+capacity, sizeof(char *)*16);
682                                         capacity += 16;
683                                         *cOptions = moreOptions;
684                                     }
685                                     else
686                                         hr = E_OUTOFMEMORY;
687                                 }
688                             }
689                             else
690                                 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
691                                     E_OUTOFMEMORY : E_INVALIDARG;
692                         }
693                         else
694                             hr = E_INVALIDARG;
695
696                         VariantClear(&option);
697                     }
698                     *cOptionCount = pos;
699                     if( FAILED(hr) )
700                     {
701                         // free already processed elements
702                         FreeTargetOptions(*cOptions, *cOptionCount);
703                     }
704                 }
705                 else
706                     hr = E_OUTOFMEMORY;
707
708                 enumVar->Release();
709             }
710         }
711         else
712         {
713             // coerce object into a string and parse it
714             VARIANT v_name;
715             VariantInit(&v_name);
716             hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
717             if( SUCCEEDED(hr) )
718             {
719                 hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
720                 VariantClear(&v_name);
721             }
722         }
723     }
724     else if( V_ISARRAY(options) )
725     {
726         // array parameter
727         SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);
728
729         if( SafeArrayGetDim(array) != 1 )
730             return E_INVALIDARG;
731
732         long lBound = 0;
733         long uBound = 0;
734         SafeArrayGetLBound(array, 1, &lBound);
735         SafeArrayGetUBound(array, 1, &uBound);
736
737         // have we got any options
738         if( uBound >= lBound )
739         {
740             VARTYPE vType;
741             hr = SafeArrayGetVartype(array, &vType);
742             if( FAILED(hr) )
743                 return hr;
744
745             long pos;
746
747             // marshall options into an array of C strings
748             if( VT_VARIANT == vType )
749             {
750                 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
751                 if( NULL == *cOptions )
752                     return E_OUTOFMEMORY;
753
754                 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
755                 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
756                 {
757                     VARIANT option;
758                     hr = SafeArrayGetElement(array, &pos, &option);
759                     if( SUCCEEDED(hr) )
760                     {
761                         if( VT_BSTR == V_VT(&option) )
762                         {
763                             char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
764                             (*cOptions)[pos-lBound] = cOption;
765                             if( NULL == cOption )
766                                 hr = ( SysStringLen(V_BSTR(&option)) > 0 ) ?
767                                     E_OUTOFMEMORY : E_INVALIDARG;
768                         }
769                         else
770                             hr = E_INVALIDARG;
771                         VariantClear(&option);
772                     }
773                 }
774             }
775             else if( VT_BSTR == vType )
776             {
777                 *cOptions = (char **)CoTaskMemAlloc(sizeof(char *)*(uBound-lBound+1));
778                 if( NULL == *cOptions )
779                     return E_OUTOFMEMORY;
780
781                 ZeroMemory(*cOptions, sizeof(char *)*(uBound-lBound+1));
782                 for(pos=lBound; (pos<=uBound) && SUCCEEDED(hr); ++pos )
783                 {
784                     BSTR option;
785                     hr = SafeArrayGetElement(array, &pos, &option);
786                     if( SUCCEEDED(hr) )
787                     {
788                         char *cOption = CStrFromBSTR(codePage, option);
789
790                         (*cOptions)[pos-lBound] = cOption;
791                         if( NULL == cOption )
792                             hr = ( SysStringLen(option) > 0 ) ?
793                                 E_OUTOFMEMORY : E_INVALIDARG;
794                         SysFreeString(option);
795                     }
796                 }
797             }
798             else
799             {
800                 // unsupported type
801                 return E_INVALIDARG;
802             }
803
804             *cOptionCount = pos-lBound;
805             if( FAILED(hr) )
806             {
807                 // free already processed elements
808                 FreeTargetOptions(*cOptions, *cOptionCount);
809             }
810         }
811         else
812         {
813             // empty array
814             *cOptions = NULL;
815             *cOptionCount = 0;
816             return NOERROR;
817         }
818     }
819     else if( VT_UNKNOWN == V_VT(options) )
820     {
821         // coerce object into a string and parse it
822         VARIANT v_name;
823         VariantInit(&v_name);
824         hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
825         if( SUCCEEDED(hr) )
826         {
827             hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
828             VariantClear(&v_name);
829         }
830     }
831     else if( VT_BSTR == V_VT(options) )
832     {
833         hr = parseStringOptions(codePage, V_BSTR(options), cOptions, cOptionCount);
834     }
835     return hr;
836 };
837
838 /*
839 ** use VARIANT rather than a SAFEARRAY as argument type
840 ** for compatibility with some scripting language (JScript)
841 */
842
843 STDMETHODIMP VLCControl::addTarget(BSTR uri, VARIANT options, enum VLCPlaylistMode mode, int position)
844 {
845     if( 0 == SysStringLen(uri) )
846         return E_INVALIDARG;
847
848     libvlc_instance_t *p_libvlc;
849     HRESULT hr = _p_instance->getVLC(&p_libvlc);
850     if( SUCCEEDED(hr) )
851     {
852         char *cUri = CStrFromBSTR(CP_UTF8, uri);
853         if( NULL == cUri )
854             return E_OUTOFMEMORY;
855
856         int cOptionsCount;
857         char **cOptions;
858
859         if( FAILED(CreateTargetOptions(CP_UTF8, &options, &cOptions, &cOptionsCount)) )
860             return E_INVALIDARG;
861
862         libvlc_exception_t ex;
863         libvlc_exception_init(&ex);
864
865         position = libvlc_playlist_add_extended(p_libvlc, cUri, cUri,
866                                                 cOptionsCount,
867                                                 const_cast<const char**>(cOptions),
868                                                 &ex);
869
870         FreeTargetOptions(cOptions, cOptionsCount);
871         CoTaskMemFree(cUri);
872
873         if( libvlc_exception_raised(&ex) )
874         {
875             _p_instance->setErrorInfo(IID_IVLCPlaylist,
876                 libvlc_exception_get_message(&ex));
877             libvlc_exception_clear(&ex);
878
879             if( mode & VLCPlayListAppendAndGo )
880                 _p_instance->fireOnStopEvent();
881             return E_FAIL;
882         }
883
884         if( mode & VLCPlayListAppendAndGo )
885             _p_instance->fireOnPlayEvent();
886         return NOERROR;
887     }
888     return hr;
889 };
890
891 STDMETHODIMP VLCControl::get_PlaylistIndex(int *index)
892 {
893     return E_INVALIDARG;
894 #if 0
895     if( NULL == index )
896         return E_POINTER;
897
898     libvlc_instance_t *p_libvlc;
899     HRESULT result = _p_instance->getVLC(&p_libvlc);
900     if( SUCCEEDED(result) )
901     {
902         *index = VLC_PlaylistIndex(i_vlc);
903         return NOERROR;
904     }
905     *index = 0;
906     return result;
907 #endif
908 };
909
910 STDMETHODIMP VLCControl::get_PlaylistCount(int *count)
911 {
912     if( NULL == count )
913         return E_POINTER;
914
915     *count = 0;
916     libvlc_instance_t* p_libvlc;
917     HRESULT result = _p_instance->getVLC(&p_libvlc);
918     if( SUCCEEDED(result) )
919     {
920         libvlc_exception_t ex;
921         libvlc_exception_init(&ex);
922
923         *count = libvlc_playlist_items_count(p_libvlc, &ex);
924         if( libvlc_exception_raised(&ex) )
925         {
926             _p_instance->setErrorInfo(IID_IVLCControl,
927                 libvlc_exception_get_message(&ex));
928             libvlc_exception_clear(&ex);
929             return E_FAIL;
930         }
931         return NOERROR;
932     }
933     return result;
934 };
935
936 STDMETHODIMP VLCControl::playlistNext(void)
937 {
938     libvlc_instance_t* p_libvlc;
939     HRESULT result = _p_instance->getVLC(&p_libvlc);
940     if( SUCCEEDED(result) )
941     {
942         libvlc_exception_t ex;
943         libvlc_exception_init(&ex);
944
945         libvlc_playlist_next(p_libvlc, &ex);
946         if( libvlc_exception_raised(&ex) )
947         {
948             _p_instance->setErrorInfo(IID_IVLCControl,
949                 libvlc_exception_get_message(&ex));
950             libvlc_exception_clear(&ex);
951             return E_FAIL;
952         }
953         return NOERROR;
954     }
955     return result;
956 };
957
958 STDMETHODIMP VLCControl::playlistPrev(void)
959 {
960     libvlc_instance_t* p_libvlc;
961     HRESULT result = _p_instance->getVLC(&p_libvlc);
962     if( SUCCEEDED(result) )
963     {
964         libvlc_exception_t ex;
965         libvlc_exception_init(&ex);
966
967         libvlc_playlist_prev(p_libvlc, &ex);
968         if( libvlc_exception_raised(&ex) )
969         {
970             _p_instance->setErrorInfo(IID_IVLCControl,
971                 libvlc_exception_get_message(&ex));
972             libvlc_exception_clear(&ex);
973             return E_FAIL;
974         }
975         return NOERROR;
976     }
977     return result;
978 };
979
980 STDMETHODIMP VLCControl::playlistClear(void)
981 {
982     libvlc_instance_t* p_libvlc;
983     HRESULT result = _p_instance->getVLC(&p_libvlc);
984     if( SUCCEEDED(result) )
985     {
986         libvlc_exception_t ex;
987         libvlc_exception_init(&ex);
988
989         libvlc_playlist_clear(p_libvlc, &ex);
990         if( libvlc_exception_raised(&ex) )
991         {
992             _p_instance->setErrorInfo(IID_IVLCControl,
993                 libvlc_exception_get_message(&ex));
994             libvlc_exception_clear(&ex);
995             return E_FAIL;
996         }
997         return NOERROR;
998     }
999     return result;
1000 };
1001
1002 STDMETHODIMP VLCControl::get_VersionInfo(BSTR *version)
1003 {
1004     if( NULL == version )
1005         return E_POINTER;
1006
1007     const char *versionStr = libvlc_get_version();
1008     if( NULL != versionStr )
1009     {
1010         *version = BSTRFromCStr(CP_UTF8, versionStr);
1011         return (NULL == *version) ? E_OUTOFMEMORY : NOERROR;
1012     }
1013     *version = NULL;
1014     return E_FAIL;
1015 };
1016
1017 STDMETHODIMP VLCControl::get_MRL(BSTR *mrl)
1018 {
1019     if( NULL == mrl )
1020         return E_POINTER;
1021
1022     *mrl = SysAllocStringLen(_p_instance->getMRL(),
1023                 SysStringLen(_p_instance->getMRL()));
1024     return NOERROR;
1025 };
1026
1027 STDMETHODIMP VLCControl::put_MRL(BSTR mrl)
1028 {
1029     _p_instance->setMRL(mrl);
1030
1031     return S_OK;
1032 };
1033
1034 STDMETHODIMP VLCControl::get_AutoPlay(VARIANT_BOOL *autoplay)
1035 {
1036     if( NULL == autoplay )
1037         return E_POINTER;
1038
1039     *autoplay = _p_instance->getAutoPlay() ? VARIANT_TRUE: VARIANT_FALSE;
1040     return S_OK;
1041 };
1042
1043 STDMETHODIMP VLCControl::put_AutoPlay(VARIANT_BOOL autoplay)
1044 {
1045     _p_instance->setAutoPlay((VARIANT_FALSE != autoplay) ? TRUE: FALSE);
1046     return S_OK;
1047 };
1048
1049 STDMETHODIMP VLCControl::get_AutoLoop(VARIANT_BOOL *autoloop)
1050 {
1051     if( NULL == autoloop )
1052         return E_POINTER;
1053
1054     *autoloop = _p_instance->getAutoLoop() ? VARIANT_TRUE: VARIANT_FALSE;
1055     return S_OK;
1056 };
1057
1058 STDMETHODIMP VLCControl::put_AutoLoop(VARIANT_BOOL autoloop)
1059 {
1060     _p_instance->setAutoLoop((VARIANT_FALSE != autoloop) ? TRUE: FALSE);
1061     return S_OK;
1062 };