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