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