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