]> git.sesse.net Git - vlc/blob - projects/activex/plugin.cpp
cc99dc7c2d7fc1bb46bd0487cbeb2c9e4035c401
[vlc] / projects / activex / plugin.cpp
1 /*****************************************************************************
2  * plugin.cpp: ActiveX control for VLC
3  *****************************************************************************
4  * Copyright (C) 2006-2010 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
26 #include "oleobject.h"
27 #include "olecontrol.h"
28 #include "oleinplaceobject.h"
29 #include "oleinplaceactiveobject.h"
30 #include "persistpropbag.h"
31 #include "persiststreaminit.h"
32 #include "persiststorage.h"
33 #include "provideclassinfo.h"
34 #include "connectioncontainer.h"
35 #include "objectsafety.h"
36 #include "vlccontrol.h"
37 #include "vlccontrol2.h"
38 #include "viewobject.h"
39 #include "dataobject.h"
40 #include "supporterrorinfo.h"
41
42 #include "utils.h"
43
44 #include <stdio.h>
45 #include <string.h>
46 #include <winreg.h>
47 #include <winuser.h>
48 #include <servprov.h>
49 #include <shlwapi.h>
50 #include <wininet.h>
51 #include <assert.h>
52
53 using namespace std;
54
55 ////////////////////////////////////////////////////////////////////////
56 //class factory
57
58 static LRESULT CALLBACK VLCInPlaceClassWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
59     VLCPlugin *p_instance = reinterpret_cast<VLCPlugin *>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
60
61     switch( uMsg )
62     {
63         case WM_ERASEBKGND:
64             return 1L;
65
66         case WM_PAINT:
67             PAINTSTRUCT ps;
68             RECT pr;
69             if( GetUpdateRect(hWnd, &pr, FALSE) )
70             {
71                 RECT bounds;
72                 GetClientRect(hWnd, &bounds);
73                 BeginPaint(hWnd, &ps);
74                 p_instance->onPaint(ps.hdc, bounds, pr);
75                 EndPaint(hWnd, &ps);
76             }
77             return 0L;
78
79         default:
80             return DefWindowProc(hWnd, uMsg, wParam, lParam);
81     }
82 };
83
84 VLCPluginClass::VLCPluginClass(LONG *p_class_ref, HINSTANCE hInstance, REFCLSID rclsid) :
85     _p_class_ref(p_class_ref),
86     _hinstance(hInstance),
87     _classid(rclsid),
88     _inplace_picture(NULL)
89 {
90     WNDCLASS wClass;
91
92     if( ! GetClassInfo(hInstance, getInPlaceWndClassName(), &wClass) )
93     {
94         wClass.style          = CS_NOCLOSE|CS_DBLCLKS;
95         wClass.lpfnWndProc    = VLCInPlaceClassWndProc;
96         wClass.cbClsExtra     = 0;
97         wClass.cbWndExtra     = 0;
98         wClass.hInstance      = hInstance;
99         wClass.hIcon          = NULL;
100         wClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
101         wClass.hbrBackground  = NULL;
102         wClass.lpszMenuName   = NULL;
103         wClass.lpszClassName  = getInPlaceWndClassName();
104
105         _inplace_wndclass_atom = RegisterClass(&wClass);
106     }
107     else
108     {
109         _inplace_wndclass_atom = 0;
110     }
111
112     HBITMAP hbitmap = (HBITMAP)LoadImage(getHInstance(), MAKEINTRESOURCE(2), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
113     if( NULL != hbitmap )
114     {
115         PICTDESC pictDesc;
116
117         pictDesc.cbSizeofstruct = sizeof(PICTDESC);
118         pictDesc.picType        = PICTYPE_BITMAP;
119         pictDesc.bmp.hbitmap    = hbitmap;
120         pictDesc.bmp.hpal       = NULL;
121
122         if( FAILED(OleCreatePictureIndirect(&pictDesc, IID_IPicture, TRUE, reinterpret_cast<LPVOID*>(&_inplace_picture))) )
123             _inplace_picture = NULL;
124     }
125     AddRef();
126 };
127
128 VLCPluginClass::~VLCPluginClass()
129 {
130     if( 0 != _inplace_wndclass_atom )
131         UnregisterClass(MAKEINTATOM(_inplace_wndclass_atom), _hinstance);
132
133     if( NULL != _inplace_picture )
134         _inplace_picture->Release();
135 };
136
137 STDMETHODIMP VLCPluginClass::QueryInterface(REFIID riid, void **ppv)
138 {
139     if( NULL == ppv )
140         return E_INVALIDARG;
141
142     if( (IID_IUnknown == riid)
143      || (IID_IClassFactory == riid) )
144     {
145         AddRef();
146         *ppv = reinterpret_cast<LPVOID>(this);
147
148         return NOERROR;
149     }
150
151     *ppv = NULL;
152
153     return E_NOINTERFACE;
154 };
155
156 STDMETHODIMP_(ULONG) VLCPluginClass::AddRef(void)
157 {
158     return InterlockedIncrement(_p_class_ref);
159 };
160
161 STDMETHODIMP_(ULONG) VLCPluginClass::Release(void)
162 {
163     ULONG refcount = InterlockedDecrement(_p_class_ref);
164     if( 0 == refcount )
165     {
166         delete this;
167         return 0;
168     }
169     return refcount;
170 };
171
172 STDMETHODIMP VLCPluginClass::CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, void **ppv)
173 {
174     if( NULL == ppv )
175         return E_POINTER;
176
177     *ppv = NULL;
178
179     if( (NULL != pUnkOuter) && (IID_IUnknown != riid) ) {
180         return CLASS_E_NOAGGREGATION;
181     }
182
183     VLCPlugin *plugin = new VLCPlugin(this, pUnkOuter);
184     if( NULL != plugin )
185     {
186         HRESULT hr = plugin->QueryInterface(riid, ppv);
187         // the following will destroy the object if QueryInterface() failed
188         plugin->Release();
189         return hr;
190     }
191     return E_OUTOFMEMORY;
192 };
193
194 STDMETHODIMP VLCPluginClass::LockServer(BOOL fLock)
195 {
196     if( fLock )
197         AddRef();
198     else
199         Release();
200
201     return S_OK;
202 };
203
204 ////////////////////////////////////////////////////////////////////////
205
206 VLCPlugin::VLCPlugin(VLCPluginClass *p_class, LPUNKNOWN pUnkOuter) :
207     _inplacewnd(NULL),
208     _p_class(p_class),
209     _i_ref(1UL),
210     _p_libvlc(NULL),
211     _p_mlist(NULL),
212     _p_mplayer(NULL),
213     _i_midx(-1),
214     _i_codepage(CP_ACP),
215     _b_usermode(TRUE)
216 {
217     p_class->AddRef();
218
219     vlcOleControl = new VLCOleControl(this);
220     vlcOleInPlaceObject = new VLCOleInPlaceObject(this);
221     vlcOleInPlaceActiveObject = new VLCOleInPlaceActiveObject(this);
222     vlcPersistStorage = new VLCPersistStorage(this);
223     vlcPersistStreamInit = new VLCPersistStreamInit(this);
224     vlcPersistPropertyBag = new VLCPersistPropertyBag(this);
225     vlcProvideClassInfo = new VLCProvideClassInfo(this);
226     vlcConnectionPointContainer = new VLCConnectionPointContainer(this);
227     vlcObjectSafety = new VLCObjectSafety(this);
228     vlcControl = new VLCControl(this);
229     vlcControl2 = new VLCControl2(this);
230     vlcViewObject = new VLCViewObject(this);
231     vlcDataObject = new VLCDataObject(this);
232     vlcOleObject = new VLCOleObject(this);
233     vlcSupportErrorInfo = new VLCSupportErrorInfo(this);
234
235     // configure controlling IUnknown interface for implemented interfaces
236     this->pUnkOuter = (NULL != pUnkOuter) ? pUnkOuter : dynamic_cast<LPUNKNOWN>(this);
237
238     // default picure
239     _p_pict = p_class->getInPlacePict();
240
241     // make sure that persistable properties are initialized
242     onInit();
243 };
244
245 VLCPlugin::~VLCPlugin()
246 {
247     /*
248     ** bump refcount to avoid recursive release from
249     ** following interfaces when releasing this interface
250     */
251     AddRef();
252
253     delete vlcSupportErrorInfo;
254     delete vlcOleObject;
255     delete vlcDataObject;
256     delete vlcViewObject;
257     delete vlcControl2;
258     delete vlcControl;
259     delete vlcConnectionPointContainer;
260     delete vlcProvideClassInfo;
261     delete vlcPersistPropertyBag;
262     delete vlcPersistStreamInit;
263     delete vlcPersistStorage;
264     delete vlcOleInPlaceActiveObject;
265     delete vlcOleInPlaceObject;
266     delete vlcObjectSafety;
267
268     delete vlcOleControl;
269     if( _p_pict )
270         _p_pict->Release();
271
272     SysFreeString(_bstr_mrl);
273     SysFreeString(_bstr_baseurl);
274
275     if( _p_mplayer )
276     {
277         if( isPlaying() )
278             playlist_stop();
279
280         player_unregister_events();
281         libvlc_media_player_release(_p_mplayer);
282         _p_mplayer=NULL;
283     }
284     if( _p_mlist )   { libvlc_media_list_release(_p_mlist); _p_mlist=NULL; }
285     if( _p_libvlc )  { libvlc_release(_p_libvlc); _p_libvlc=NULL; }
286
287     _p_class->Release();
288 };
289
290 STDMETHODIMP VLCPlugin::QueryInterface(REFIID riid, void **ppv)
291 {
292     if( NULL == ppv )
293         return E_INVALIDARG;
294
295     if( IID_IUnknown == riid )
296         *ppv = reinterpret_cast<LPVOID>(this);
297     else if( IID_IOleObject == riid )
298         *ppv = reinterpret_cast<LPVOID>(vlcOleObject);
299     else if( IID_IOleControl == riid )
300         *ppv = reinterpret_cast<LPVOID>(vlcOleControl);
301     else if( IID_IOleWindow == riid )
302         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
303     else if( IID_IOleInPlaceObject == riid )
304         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
305     else if( IID_IOleInPlaceActiveObject == riid )
306         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceActiveObject);
307     else if( IID_IPersist == riid )
308         *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
309     else if( IID_IPersistStreamInit == riid )
310         *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
311     else if( IID_IPersistStorage == riid )
312         *ppv = reinterpret_cast<LPVOID>(vlcPersistStorage);
313     else if( IID_IPersistPropertyBag == riid )
314         *ppv = reinterpret_cast<LPVOID>(vlcPersistPropertyBag);
315     else if( IID_IProvideClassInfo == riid )
316         *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
317     else if( IID_IProvideClassInfo2 == riid )
318         *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
319     else if( IID_IConnectionPointContainer == riid )
320         *ppv = reinterpret_cast<LPVOID>(vlcConnectionPointContainer);
321     else if( IID_IObjectSafety == riid )
322         *ppv = reinterpret_cast<LPVOID>(vlcObjectSafety);
323     else if( IID_IDispatch == riid )
324         *ppv = (CLSID_VLCPlugin2 == getClassID()) ?
325                 reinterpret_cast<LPVOID>(vlcControl2) :
326                 reinterpret_cast<LPVOID>(vlcControl);
327     else if( IID_IVLCControl == riid )
328         *ppv = reinterpret_cast<LPVOID>(vlcControl);
329     else if( IID_IVLCControl2 == riid )
330         *ppv = reinterpret_cast<LPVOID>(vlcControl2);
331     else if( IID_IViewObject == riid )
332         *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
333     else if( IID_IViewObject2 == riid )
334         *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
335     else if( IID_IDataObject == riid )
336         *ppv = reinterpret_cast<LPVOID>(vlcDataObject);
337     else if( IID_ISupportErrorInfo == riid )
338         *ppv = reinterpret_cast<LPVOID>(vlcSupportErrorInfo);
339     else
340     {
341         *ppv = NULL;
342         return E_NOINTERFACE;
343     }
344     ((LPUNKNOWN)*ppv)->AddRef();
345     return NOERROR;
346 };
347
348 STDMETHODIMP_(ULONG) VLCPlugin::AddRef(void)
349 {
350     return InterlockedIncrement((LONG *)&_i_ref);
351 };
352
353 STDMETHODIMP_(ULONG) VLCPlugin::Release(void)
354 {
355     if( ! InterlockedDecrement((LONG *)&_i_ref) )
356     {
357         delete this;
358         return 0;
359     }
360     return _i_ref;
361 };
362
363 //////////////////////////////////////
364
365 HRESULT VLCPlugin::onInit(void)
366 {
367     if( NULL == _p_libvlc )
368     {
369         // initialize persistable properties
370         _b_autoplay   = TRUE;
371         _b_autoloop   = FALSE;
372         _b_toolbar    = FALSE;
373         _bstr_baseurl = NULL;
374         _bstr_mrl     = NULL;
375         _b_visible    = TRUE;
376         _b_mute       = FALSE;
377         _i_volume     = 50;
378         _i_time       = 0;
379         _i_backcolor  = 0;
380         // set default/preferred size (320x240) pixels in HIMETRIC
381         HDC hDC = CreateDevDC(NULL);
382         _extent.cx = 320;
383         _extent.cy = 240;
384         HimetricFromDP(hDC, (LPPOINT)&_extent, 1);
385         DeleteDC(hDC);
386
387         return S_OK;
388     }
389     return CO_E_ALREADYINITIALIZED;
390 };
391
392 HRESULT VLCPlugin::onLoad(void)
393 {
394     if( SysStringLen(_bstr_baseurl) == 0 )
395     {
396         /*
397         ** try to retreive the base URL using the client site moniker, which for Internet Explorer
398         ** is the URL of the page the plugin is embedded into.
399         */
400         LPOLECLIENTSITE pClientSite;
401         if( SUCCEEDED(vlcOleObject->GetClientSite(&pClientSite)) && (NULL != pClientSite) )
402         {
403             IBindCtx *pBC = 0;
404             if( SUCCEEDED(CreateBindCtx(0, &pBC)) )
405             {
406                 LPMONIKER pContMoniker = NULL;
407                 if( SUCCEEDED(pClientSite->GetMoniker(OLEGETMONIKER_ONLYIFTHERE,
408                                 OLEWHICHMK_CONTAINER, &pContMoniker)) )
409                 {
410                     LPOLESTR base_url;
411                     if( SUCCEEDED(pContMoniker->GetDisplayName(pBC, NULL, &base_url)) )
412                     {
413                         /*
414                         ** check that the moniker name is a URL
415                         */
416                         if( UrlIsW(base_url, URLIS_URL) )
417                         {
418                             /* copy base URL */
419                             _bstr_baseurl = SysAllocString(base_url);
420                         }
421                         CoTaskMemFree(base_url);
422                     }
423                 }
424             }
425         }
426     }
427     setDirty(FALSE);
428     return S_OK;
429 };
430
431 void VLCPlugin::initVLC()
432 {
433     extern HMODULE DllGetModule();
434
435     /*
436     ** default initialization options
437     */
438     const char *ppsz_argv[32] = { };
439     int   ppsz_argc = 0;
440
441     char p_progpath[MAX_PATH];
442     {
443         TCHAR w_progpath[MAX_PATH];
444         DWORD len = GetModuleFileName(DllGetModule(), w_progpath, MAX_PATH);
445         if( len > 0 )
446         {
447             len = WideCharToMultiByte(CP_UTF8, 0, w_progpath, len, p_progpath,
448                        sizeof(p_progpath)-1, NULL, NULL);
449             if( len > 0 )
450             {
451                 p_progpath[len] = '\0';
452                 ppsz_argv[0] = p_progpath;
453             }
454         }
455     }
456
457     ppsz_argv[ppsz_argc++] = "-vv";
458
459     HKEY h_key;
460     char p_pluginpath[MAX_PATH];
461     if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, TEXT("Software\\VideoLAN\\VLC"),
462                       0, KEY_READ, &h_key ) == ERROR_SUCCESS )
463     {
464         DWORD i_type, i_data = MAX_PATH;
465         TCHAR w_pluginpath[MAX_PATH];
466         if( RegQueryValueEx( h_key, TEXT("InstallDir"), 0, &i_type,
467                              (LPBYTE)w_pluginpath, &i_data ) == ERROR_SUCCESS )
468         {
469             if( i_type == REG_SZ )
470             {
471                 if( WideCharToMultiByte(CP_UTF8, 0, w_pluginpath, -1, p_pluginpath,
472                          sizeof(p_pluginpath)-sizeof("\\plugins")+1, NULL, NULL) )
473                 {
474                     strcat( p_pluginpath, "\\plugins" );
475                     ppsz_argv[ppsz_argc++] = "--plugin-path";
476                     ppsz_argv[ppsz_argc++] = p_pluginpath;
477                 }
478             }
479         }
480         RegCloseKey( h_key );
481     }
482
483     // make sure plugin isn't affected with VLC single instance mode
484     ppsz_argv[ppsz_argc++] = "--no-one-instance";
485
486     /* common settings */
487     ppsz_argv[ppsz_argc++] = "-vv";
488     ppsz_argv[ppsz_argc++] = "--no-stats";
489     ppsz_argv[ppsz_argc++] = "--no-media-library";
490     ppsz_argv[ppsz_argc++] = "--intf=dummy";
491     ppsz_argv[ppsz_argc++] = "--no-video-title-show";
492
493
494     // loop mode is a configuration option only
495     if( _b_autoloop )
496         ppsz_argv[ppsz_argc++] = "--loop";
497
498     _p_libvlc = libvlc_new(ppsz_argc, ppsz_argv);
499     if( !_p_libvlc )
500         return;
501
502     _p_mlist = libvlc_media_list_new(_p_libvlc);
503
504     // initial playlist item
505     if( SysStringLen(_bstr_mrl) > 0 )
506     {
507         char *psz_mrl = NULL;
508
509         if( SysStringLen(_bstr_baseurl) > 0 )
510         {
511             /*
512             ** if the MRL a relative URL, we should end up with an absolute URL
513             */
514             LPWSTR abs_url = CombineURL(_bstr_baseurl, _bstr_mrl);
515             if( NULL != abs_url )
516             {
517                 psz_mrl = CStrFromWSTR(CP_UTF8, abs_url, wcslen(abs_url));
518                 CoTaskMemFree(abs_url);
519             }
520             else
521             {
522                 psz_mrl = CStrFromBSTR(CP_UTF8, _bstr_mrl);
523             }
524         }
525         else
526         {
527             /*
528             ** baseURL is empty, assume MRL is absolute
529             */
530             psz_mrl = CStrFromBSTR(CP_UTF8, _bstr_mrl);
531         }
532         if( NULL != psz_mrl )
533         {
534             const char *options[1];
535             int i_options = 0;
536
537             char timeBuffer[32];
538             if( _i_time )
539             {
540                 snprintf(timeBuffer, sizeof(timeBuffer), ":start-time=%d", _i_time);
541                 options[i_options++] = timeBuffer;
542             }
543             // add default target to playlist
544             playlist_add_extended_untrusted(psz_mrl, i_options, options);
545             CoTaskMemFree(psz_mrl);
546         }
547     }
548 };
549
550 void VLCPlugin::setErrorInfo(REFIID riid, const char *description)
551 {
552     vlcSupportErrorInfo->setErrorInfo( getClassID() == CLSID_VLCPlugin2 ?
553         OLESTR("VideoLAN.VLCPlugin.2") : OLESTR("VideoLAN.VLCPlugin.1"),
554         riid, description );
555 };
556
557 HRESULT VLCPlugin::onAmbientChanged(LPUNKNOWN pContainer, DISPID dispID)
558 {
559     VARIANT v;
560     switch( dispID )
561     {
562         case DISPID_AMBIENT_BACKCOLOR:
563             VariantInit(&v);
564             V_VT(&v) = VT_I4;
565             if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
566             {
567                 setBackColor(V_I4(&v));
568             }
569             break;
570         case DISPID_AMBIENT_DISPLAYNAME:
571             break;
572         case DISPID_AMBIENT_FONT:
573             break;
574         case DISPID_AMBIENT_FORECOLOR:
575             break;
576         case DISPID_AMBIENT_LOCALEID:
577             break;
578         case DISPID_AMBIENT_MESSAGEREFLECT:
579             break;
580         case DISPID_AMBIENT_SCALEUNITS:
581             break;
582         case DISPID_AMBIENT_TEXTALIGN:
583             break;
584         case DISPID_AMBIENT_USERMODE:
585             VariantInit(&v);
586             V_VT(&v) = VT_BOOL;
587             if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
588             {
589                 setUserMode(V_BOOL(&v) != VARIANT_FALSE);
590             }
591             break;
592         case DISPID_AMBIENT_UIDEAD:
593             break;
594         case DISPID_AMBIENT_SHOWGRABHANDLES:
595             break;
596         case DISPID_AMBIENT_SHOWHATCHING:
597             break;
598         case DISPID_AMBIENT_DISPLAYASDEFAULT:
599             break;
600         case DISPID_AMBIENT_SUPPORTSMNEMONICS:
601             break;
602         case DISPID_AMBIENT_AUTOCLIP:
603             break;
604         case DISPID_AMBIENT_APPEARANCE:
605             break;
606         case DISPID_AMBIENT_CODEPAGE:
607             VariantInit(&v);
608             V_VT(&v) = VT_I4;
609             if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
610             {
611                 setCodePage(V_I4(&v));
612             }
613             break;
614         case DISPID_AMBIENT_PALETTE:
615             break;
616         case DISPID_AMBIENT_CHARSET:
617             break;
618         case DISPID_AMBIENT_RIGHTTOLEFT:
619             break;
620         case DISPID_AMBIENT_TOPTOBOTTOM:
621             break;
622         case DISPID_UNKNOWN:
623             /*
624             ** multiple property change, look up the ones we are interested in
625             */
626             VariantInit(&v);
627             V_VT(&v) = VT_BOOL;
628             if( SUCCEEDED(GetObjectProperty(pContainer, DISPID_AMBIENT_USERMODE, v)) )
629             {
630                 setUserMode(V_BOOL(&v) != VARIANT_FALSE);
631             }
632             VariantInit(&v);
633             V_VT(&v) = VT_I4;
634             if( SUCCEEDED(GetObjectProperty(pContainer, DISPID_AMBIENT_CODEPAGE, v)) )
635             {
636                 setCodePage(V_I4(&v));
637             }
638             break;
639     }
640     return S_OK;
641 };
642
643 HRESULT VLCPlugin::onClose(DWORD dwSaveOption)
644 {
645     if( isInPlaceActive() )
646     {
647         onInPlaceDeactivate();
648     }
649     if( isRunning() )
650     {
651         libvlc_instance_t* p_libvlc = _p_libvlc;
652
653         _p_libvlc = NULL;
654         vlcDataObject->onClose();
655
656         if( p_libvlc )
657             libvlc_release(p_libvlc);
658     }
659     return S_OK;
660 };
661
662 BOOL VLCPlugin::isInPlaceActive(void)
663 {
664     return (NULL != _inplacewnd);
665 };
666
667 HRESULT VLCPlugin::onActivateInPlace(LPMSG lpMesg, HWND hwndParent, LPCRECT lprcPosRect, LPCRECT lprcClipRect)
668 {
669     RECT clipRect = *lprcClipRect;
670
671     /*
672     ** record keeping of control geometry within container
673     */
674     _posRect = *lprcPosRect;
675
676     /*
677     ** Create a window for in place activated control.
678     ** the window geometry matches the control viewport
679     ** within container so that embedded video is always
680     ** properly displayed.
681     */
682     _inplacewnd = CreateWindow(_p_class->getInPlaceWndClassName(),
683             TEXT("VLC Plugin In-Place Window"),
684             WS_CHILD|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,
685             lprcPosRect->left,
686             lprcPosRect->top,
687             lprcPosRect->right-lprcPosRect->left,
688             lprcPosRect->bottom-lprcPosRect->top,
689             hwndParent,
690             0,
691             _p_class->getHInstance(),
692             NULL
693            );
694
695     if( NULL == _inplacewnd )
696         return E_FAIL;
697
698     SetWindowLongPtr(_inplacewnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
699
700     /* change cliprect coordinates system relative to window bounding rect */
701     OffsetRect(&clipRect, -lprcPosRect->left, -lprcPosRect->top);
702
703     HRGN clipRgn = CreateRectRgnIndirect(&clipRect);
704     SetWindowRgn(_inplacewnd, clipRgn, TRUE);
705
706     if( _b_usermode )
707     {
708         /* will run vlc if not done already */
709         libvlc_instance_t* p_libvlc;
710         HRESULT result = getVLC(&p_libvlc);
711         if( FAILED(result) )
712             return result;
713
714         if( _b_autoplay && playlist_select(0) )
715         {
716             libvlc_media_player_play(_p_mplayer);
717             fireOnPlayEvent();
718         }
719     }
720
721     if( isVisible() )
722         ShowWindow(_inplacewnd, SW_SHOW);
723
724     return S_OK;
725 };
726
727 HRESULT VLCPlugin::onInPlaceDeactivate(void)
728 {
729     if( isPlaying() )
730     {
731         playlist_stop();
732         fireOnStopEvent();
733     }
734
735     DestroyWindow(_inplacewnd);
736     _inplacewnd = NULL;
737
738     return S_OK;
739 };
740
741 void VLCPlugin::setVisible(BOOL fVisible)
742 {
743     if( fVisible != _b_visible )
744     {
745         _b_visible = fVisible;
746         if( isInPlaceActive() )
747         {
748             ShowWindow(_inplacewnd, fVisible ? SW_SHOW : SW_HIDE);
749             if( fVisible )
750                 InvalidateRect(_inplacewnd, NULL, TRUE);
751         }
752         setDirty(TRUE);
753         firePropChangedEvent(DISPID_Visible);
754     }
755 };
756
757 void VLCPlugin::setVolume(int volume)
758 {
759     if( volume < 0 )
760         volume = 0;
761     else if( volume > 200 )
762         volume = 200;
763
764     if( volume != _i_volume )
765     {
766         _i_volume = volume;
767         if( isRunning() )
768         {
769             libvlc_media_player_t *p_md;
770             HRESULT hr = getMD(&p_md);
771             if( SUCCEEDED(hr) )
772                 libvlc_audio_set_volume(p_md, _i_volume);
773         }
774         setDirty(TRUE);
775     }
776 };
777
778 void VLCPlugin::setBackColor(OLE_COLOR backcolor)
779 {
780     if( _i_backcolor != backcolor )
781     {
782         _i_backcolor = backcolor;
783         if( isInPlaceActive() )
784         {
785
786         }
787         setDirty(TRUE);
788     }
789 };
790
791 void VLCPlugin::setTime(int seconds)
792 {
793     if( seconds < 0 )
794         seconds = 0;
795
796     if( seconds != _i_time )
797     {
798         setStartTime(_i_time);
799         if( NULL != _p_mplayer )
800         {
801             libvlc_media_player_set_time(_p_mplayer, _i_time);
802         }
803     }
804 };
805
806 void VLCPlugin::setFocus(BOOL fFocus)
807 {
808     if( fFocus )
809         SetActiveWindow(_inplacewnd);
810 };
811
812 BOOL VLCPlugin::hasFocus(void)
813 {
814     return GetActiveWindow() == _inplacewnd;
815 };
816
817 void VLCPlugin::onDraw(DVTARGETDEVICE * ptd, HDC hicTargetDev,
818         HDC hdcDraw, LPCRECTL lprcBounds, LPCRECTL lprcWBounds)
819 {
820     if( isVisible() )
821     {
822         long width = lprcBounds->right-lprcBounds->left;
823         long height = lprcBounds->bottom-lprcBounds->top;
824
825         RECT bounds = { lprcBounds->left, lprcBounds->top, lprcBounds->right, lprcBounds->bottom };
826
827         if( isUserMode() )
828         {
829             /* VLC is in user mode, just draw background color */
830             COLORREF colorref = RGB(0, 0, 0);
831             OleTranslateColor(_i_backcolor, (HPALETTE)GetStockObject(DEFAULT_PALETTE), &colorref);
832             if( colorref != RGB(0, 0, 0) )
833             {
834                 /* custom background */
835                 HBRUSH colorbrush = CreateSolidBrush(colorref);
836                 FillRect(hdcDraw, &bounds, colorbrush);
837                 DeleteObject((HANDLE)colorbrush);
838             }
839             else
840             {
841                 /* black background */
842                 FillRect(hdcDraw, &bounds, (HBRUSH)GetStockObject(BLACK_BRUSH));
843             }
844         }
845         else
846         {
847             /* VLC is in design mode, draw the VLC logo */
848             FillRect(hdcDraw, &bounds, (HBRUSH)GetStockObject(WHITE_BRUSH));
849
850             LPPICTURE pict = getPicture();
851             if( NULL != pict )
852             {
853                 OLE_XSIZE_HIMETRIC picWidth;
854                 OLE_YSIZE_HIMETRIC picHeight;
855
856                 pict->get_Width(&picWidth);
857                 pict->get_Height(&picHeight);
858
859                 SIZEL picSize = { picWidth, picHeight };
860
861                 if( NULL != hicTargetDev )
862                 {
863                     DPFromHimetric(hicTargetDev, (LPPOINT)&picSize, 1);
864                 }
865                 else if( NULL != (hicTargetDev = CreateDevDC(ptd)) )
866                 {
867                     DPFromHimetric(hicTargetDev, (LPPOINT)&picSize, 1);
868                     DeleteDC(hicTargetDev);
869                 }
870
871                 if( picSize.cx > width-4 )
872                     picSize.cx = width-4;
873                 if( picSize.cy > height-4 )
874                     picSize.cy = height-4;
875
876                 LONG dstX = lprcBounds->left+(width-picSize.cx)/2;
877                 LONG dstY = lprcBounds->top+(height-picSize.cy)/2;
878
879                 if( NULL != lprcWBounds )
880                 {
881                     RECT wBounds = { lprcWBounds->left, lprcWBounds->top, lprcWBounds->right, lprcWBounds->bottom };
882                     pict->Render(hdcDraw, dstX, dstY, picSize.cx, picSize.cy,
883                             0L, picHeight, picWidth, -picHeight, &wBounds);
884                 }
885                 else
886                     pict->Render(hdcDraw, dstX, dstY, picSize.cx, picSize.cy,
887                             0L, picHeight, picWidth, -picHeight, NULL);
888
889                 pict->Release();
890             }
891
892             SelectObject(hdcDraw, GetStockObject(BLACK_BRUSH));
893
894             MoveToEx(hdcDraw, bounds.left, bounds.top, NULL);
895             LineTo(hdcDraw, bounds.left+width-1, bounds.top);
896             LineTo(hdcDraw, bounds.left+width-1, bounds.top+height-1);
897             LineTo(hdcDraw, bounds.left, bounds.top+height-1);
898             LineTo(hdcDraw, bounds.left, bounds.top);
899         }
900     }
901 };
902
903 void VLCPlugin::onPaint(HDC hdc, const RECT &bounds, const RECT &clipRect)
904 {
905     if( isVisible() )
906     {
907         /* if VLC is in design mode, draw control logo */
908         HDC hdcDraw = CreateCompatibleDC(hdc);
909         if( NULL != hdcDraw )
910         {
911             SIZEL size = getExtent();
912             DPFromHimetric(hdc, (LPPOINT)&size, 1);
913             RECTL posRect = { 0, 0, size.cx, size.cy };
914
915             int width = bounds.right-bounds.left;
916             int height = bounds.bottom-bounds.top;
917
918             HBITMAP hBitmap = CreateCompatibleBitmap(hdc, width, height);
919             if( NULL != hBitmap )
920             {
921                 HBITMAP oldBmp = (HBITMAP)SelectObject(hdcDraw, hBitmap);
922
923                 if( (size.cx != width) || (size.cy != height) )
924                 {
925                     // needs to scale canvas
926                     SetMapMode(hdcDraw, MM_ANISOTROPIC);
927                     SetWindowExtEx(hdcDraw, size.cx, size.cy, NULL);
928                     SetViewportExtEx(hdcDraw, width, height, NULL);
929                 }
930
931                 onDraw(NULL, hdc, hdcDraw, &posRect, NULL);
932
933                 SetMapMode(hdcDraw, MM_TEXT);
934                 BitBlt(hdc, bounds.left, bounds.top,
935                         width, height,
936                         hdcDraw, 0, 0,
937                         SRCCOPY);
938
939                 SelectObject(hdcDraw, oldBmp);
940                 DeleteObject(hBitmap);
941             }
942             DeleteDC(hdcDraw);
943         }
944     }
945 };
946
947 void VLCPlugin::onPositionChange(LPCRECT lprcPosRect, LPCRECT lprcClipRect)
948 {
949     RECT clipRect = *lprcClipRect;
950
951     //RedrawWindow(GetParent(_inplacewnd), &_posRect, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
952
953     /*
954     ** record keeping of control geometry within container
955     */
956     _posRect = *lprcPosRect;
957
958     /*
959     ** change in-place window geometry to match clipping region
960     */
961     SetWindowPos(_inplacewnd, NULL,
962             lprcPosRect->left,
963             lprcPosRect->top,
964             lprcPosRect->right-lprcPosRect->left,
965             lprcPosRect->bottom-lprcPosRect->top,
966             SWP_NOACTIVATE|
967             SWP_NOCOPYBITS|
968             SWP_NOZORDER|
969             SWP_NOOWNERZORDER );
970
971     /* change cliprect coordinates system relative to window bounding rect */
972     OffsetRect(&clipRect, -lprcPosRect->left, -lprcPosRect->top);
973     HRGN clipRgn = CreateRectRgnIndirect(&clipRect);
974     SetWindowRgn(_inplacewnd, clipRgn, FALSE);
975
976     //RedrawWindow(_videownd, &posRect, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
977 };
978
979 void VLCPlugin::freezeEvents(BOOL freeze)
980 {
981     vlcConnectionPointContainer->freezeEvents(freeze);
982 };
983
984 void VLCPlugin::firePropChangedEvent(DISPID dispid)
985 {
986     vlcConnectionPointContainer->firePropChangedEvent(dispid);
987 };
988
989 void VLCPlugin::fireOnPlayEvent(void)
990 {
991     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
992     vlcConnectionPointContainer->fireEvent(DISPID_PlayEvent, &dispparamsNoArgs);
993 };
994
995 void VLCPlugin::fireOnPauseEvent(void)
996 {
997     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
998     vlcConnectionPointContainer->fireEvent(DISPID_PauseEvent, &dispparamsNoArgs);
999 };
1000
1001 void VLCPlugin::fireOnStopEvent(void)
1002 {
1003     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1004     vlcConnectionPointContainer->fireEvent(DISPID_StopEvent, &dispparamsNoArgs);
1005 };
1006
1007 /*
1008  * Async events
1009  */
1010 void VLCPlugin::fireOnIdleEvent()
1011 {
1012     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1013     vlcConnectionPointContainer->fireEvent(DISPID_NothingSpecialEvent, &dispparamsNoArgs);
1014 };
1015
1016 void VLCPlugin::fireOnOpeningEvent()
1017 {
1018     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1019     vlcConnectionPointContainer->fireEvent(DISPID_OpeningEvent, &dispparamsNoArgs);
1020 };
1021
1022 void VLCPlugin::fireOnBufferingEvent()
1023 {
1024     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1025     vlcConnectionPointContainer->fireEvent(DISPID_BufferingEvent, &dispparamsNoArgs);
1026 };
1027
1028 void VLCPlugin::fireOnPlayingEvent()
1029 {
1030     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1031     vlcConnectionPointContainer->fireEvent(DISPID_PlayingEvent, &dispparamsNoArgs);
1032 };
1033
1034 void VLCPlugin::fireOnPausedEvent()
1035 {
1036     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1037     vlcConnectionPointContainer->fireEvent(DISPID_PausedEvent, &dispparamsNoArgs);
1038 };
1039
1040 void VLCPlugin::fireOnErrorEvent()
1041 {
1042     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1043     vlcConnectionPointContainer->fireEvent(DISPID_EncounteredErrorEvent, &dispparamsNoArgs);
1044 };
1045
1046 void VLCPlugin::fireOnEndedEvent()
1047 {
1048     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1049     vlcConnectionPointContainer->fireEvent(DISPID_EndReachedEvent, &dispparamsNoArgs);
1050 };
1051
1052 void VLCPlugin::fireOnStoppedEvent()
1053 {
1054     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1055     vlcConnectionPointContainer->fireEvent(DISPID_StoppedEvent, &dispparamsNoArgs);
1056 };
1057
1058 void VLCPlugin::fireOnForwardEvent()
1059 {
1060     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1061     vlcConnectionPointContainer->fireEvent(DISPID_ForwardEvent, &dispparamsNoArgs);
1062 };
1063
1064 void VLCPlugin::fireOnBackwardEvent()
1065 {
1066     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1067     vlcConnectionPointContainer->fireEvent(DISPID_BackwardEvent, &dispparamsNoArgs);
1068 };
1069
1070 static void handle_input_state_event(const libvlc_event_t* event, void *param)
1071 {
1072     VLCPlugin *plugin = (VLCPlugin*)param;
1073     switch( event->type )
1074     {
1075         case libvlc_MediaPlayerNothingSpecial:
1076             plugin->fireOnIdleEvent();
1077             break;
1078         case libvlc_MediaPlayerOpening:
1079             plugin->fireOnOpeningEvent();
1080             break;
1081         case libvlc_MediaPlayerBuffering:
1082             plugin->fireOnBufferingEvent();
1083             break;
1084         case libvlc_MediaPlayerPlaying:
1085             plugin->fireOnPlayingEvent();
1086             break;
1087         case libvlc_MediaPlayerPaused:
1088             plugin->fireOnPausedEvent();
1089             break;
1090         case libvlc_MediaPlayerStopped:
1091             plugin->fireOnStoppedEvent();
1092             break;
1093         case libvlc_MediaPlayerForward:
1094             plugin->fireOnForwardEvent();
1095             break;
1096         case libvlc_MediaPlayerBackward:
1097             plugin->fireOnBackwardEvent();
1098             break;
1099         case libvlc_MediaPlayerEndReached:
1100             plugin->fireOnEndedEvent();
1101             break;
1102         case libvlc_MediaPlayerEncounteredError:
1103             plugin->fireOnErrorEvent();
1104             break;
1105     }
1106 }
1107
1108 void VLCPlugin::fireOnTimeChangedEvent(long time)
1109 {
1110     VARIANT varPos;
1111     DISPPARAMS params = { &varPos, NULL, 1, 0 };
1112     varPos.vt = VT_I4;
1113     varPos.lVal = time;
1114     vlcConnectionPointContainer->fireEvent(DISPID_TimeChangedEvent, &params);
1115 };
1116
1117 static void handle_time_changed_event(const libvlc_event_t* event, void *param)
1118 {
1119     VLCPlugin *plugin = (VLCPlugin*)param;
1120     plugin->fireOnTimeChangedEvent(event->u.media_player_time_changed.new_time);
1121 }
1122
1123 void VLCPlugin::fireOnPositionChangedEvent(long position)
1124 {
1125     VARIANT varPos;
1126     DISPPARAMS params = { &varPos, NULL, 1, 0 };
1127     varPos.vt = VT_I4;
1128     varPos.lVal = position;
1129     vlcConnectionPointContainer->fireEvent(DISPID_PositionChangedEvent, &params);
1130 };
1131
1132 static void handle_position_changed_event(const libvlc_event_t* event, void *param)
1133 {
1134     VLCPlugin *plugin = (VLCPlugin*)param;
1135     plugin->fireOnPositionChangedEvent(event->u.media_player_position_changed.new_position);
1136 }
1137
1138 #define B(val) ((val) ? 0xFFFF : 0x0000)
1139 void VLCPlugin::fireOnSeekableChangedEvent(VARIANT_BOOL seekable)
1140 {
1141     VARIANT varSeek;
1142     DISPPARAMS params = { &varSeek, NULL, 1, 0 };
1143     varSeek.vt = VT_BOOL;
1144     varSeek.boolVal = seekable;
1145     vlcConnectionPointContainer->fireEvent(DISPID_SeekableChangedEvent, &params);
1146 };
1147
1148 static void handle_seekable_changed_event(const libvlc_event_t* event, void *param)
1149 {
1150     VLCPlugin *plugin = (VLCPlugin*)param;
1151     plugin->fireOnSeekableChangedEvent(B(event->u.media_player_seekable_changed.new_seekable));
1152 }
1153
1154 void VLCPlugin::fireOnPausableChangedEvent(VARIANT_BOOL pausable)
1155 {
1156     VARIANT varPause;
1157     DISPPARAMS params = { &varPause, NULL, 1, 0 };
1158     varPause.vt = VT_BOOL;
1159     varPause.boolVal = pausable;
1160     vlcConnectionPointContainer->fireEvent(DISPID_PausableChangedEvent, &params);
1161 };
1162
1163 static void handle_pausable_changed_event(const libvlc_event_t* event, void *param)
1164 {
1165     VLCPlugin *plugin = (VLCPlugin*)param;
1166     plugin->fireOnPausableChangedEvent(B(event->u.media_player_pausable_changed.new_pausable));
1167 }
1168 #undef B
1169
1170 /* */
1171
1172 bool VLCPlugin::playlist_select( int idx )
1173 {
1174     libvlc_media_t *p_m = NULL;
1175
1176     assert(_p_mlist);
1177
1178     libvlc_media_list_lock(_p_mlist);
1179
1180     int count = libvlc_media_list_count(_p_mlist);
1181
1182     if( (idx < 0) || (idx >= count) )
1183         goto bad_unlock;
1184
1185     _i_midx = idx;
1186
1187     p_m = libvlc_media_list_item_at_index(_p_mlist,_i_midx);
1188     libvlc_media_list_unlock(_p_mlist);
1189     if( !p_m )
1190         return false;
1191
1192     if( _p_mplayer )
1193     {
1194         if( isPlaying() )
1195             playlist_stop();
1196         player_unregister_events();
1197         libvlc_media_player_release( _p_mplayer );
1198         _p_mplayer = NULL;
1199     }
1200
1201     _p_mplayer = libvlc_media_player_new_from_media(p_m);
1202     if( _p_mplayer )
1203     {
1204         // initial volume setting
1205         libvlc_audio_set_volume(_p_mplayer, _i_volume);
1206         if( _b_mute )
1207             libvlc_audio_set_mute(_p_mplayer, TRUE);
1208         set_player_window();
1209         player_register_events();
1210     }
1211
1212     libvlc_media_release(p_m);
1213     return _p_mplayer ? true : false;
1214
1215 bad_unlock:
1216     libvlc_media_list_unlock(_p_mlist);
1217     return false;
1218 }
1219
1220 void VLCPlugin::set_player_window()
1221 {
1222     // XXX FIXME no idea if this is correct or not
1223     libvlc_media_player_set_hwnd(_p_mplayer,getInPlaceWindow());
1224 }
1225
1226 void VLCPlugin::player_register_events()
1227 {
1228     libvlc_event_manager_t *eventManager = NULL;
1229     assert(_p_mplayer);
1230
1231     eventManager = libvlc_media_player_event_manager(_p_mplayer);
1232     if(eventManager) {
1233         libvlc_event_attach(eventManager, libvlc_MediaPlayerNothingSpecial,
1234                             handle_input_state_event, this);
1235         libvlc_event_attach(eventManager, libvlc_MediaPlayerOpening,
1236                             handle_input_state_event, this);
1237         libvlc_event_attach(eventManager, libvlc_MediaPlayerBuffering,
1238                             handle_input_state_event, this);
1239         libvlc_event_attach(eventManager, libvlc_MediaPlayerPlaying,
1240                             handle_input_state_event, this);
1241         libvlc_event_attach(eventManager, libvlc_MediaPlayerPaused,
1242                             handle_input_state_event, this);
1243         libvlc_event_attach(eventManager, libvlc_MediaPlayerStopped,
1244                             handle_input_state_event, this);
1245         libvlc_event_attach(eventManager, libvlc_MediaPlayerForward,
1246                             handle_input_state_event, this);
1247         libvlc_event_attach(eventManager, libvlc_MediaPlayerBackward,
1248                             handle_input_state_event, this);
1249         libvlc_event_attach(eventManager, libvlc_MediaPlayerEndReached,
1250                             handle_input_state_event, this);
1251         libvlc_event_attach(eventManager, libvlc_MediaPlayerEncounteredError,
1252                             handle_input_state_event, this);
1253
1254         libvlc_event_attach(eventManager, libvlc_MediaPlayerTimeChanged,
1255                             handle_time_changed_event, this);
1256         libvlc_event_attach(eventManager, libvlc_MediaPlayerPositionChanged,
1257                             handle_position_changed_event, this);
1258         libvlc_event_attach(eventManager, libvlc_MediaPlayerSeekableChanged,
1259                             handle_seekable_changed_event, this);
1260         libvlc_event_attach(eventManager, libvlc_MediaPlayerPausableChanged,
1261                             handle_pausable_changed_event, this);
1262     }
1263 }
1264
1265 void VLCPlugin::player_unregister_events()
1266 {
1267     libvlc_event_manager_t *eventManager = NULL;
1268     assert(_p_mplayer);
1269
1270     eventManager = libvlc_media_player_event_manager(_p_mplayer);
1271     if(eventManager) {
1272         libvlc_event_detach(eventManager, libvlc_MediaPlayerNothingSpecial,
1273                             handle_input_state_event, this);
1274         libvlc_event_detach(eventManager, libvlc_MediaPlayerOpening,
1275                             handle_input_state_event, this);
1276         libvlc_event_detach(eventManager, libvlc_MediaPlayerBuffering,
1277                             handle_input_state_event, this);
1278         libvlc_event_detach(eventManager, libvlc_MediaPlayerPlaying,
1279                             handle_input_state_event, this);
1280         libvlc_event_detach(eventManager, libvlc_MediaPlayerPaused,
1281                             handle_input_state_event, this);
1282         libvlc_event_detach(eventManager, libvlc_MediaPlayerStopped,
1283                             handle_input_state_event, this);
1284         libvlc_event_detach(eventManager, libvlc_MediaPlayerForward,
1285                             handle_input_state_event, this);
1286         libvlc_event_detach(eventManager, libvlc_MediaPlayerBackward,
1287                             handle_input_state_event, this);
1288         libvlc_event_detach(eventManager, libvlc_MediaPlayerEndReached,
1289                             handle_input_state_event, this);
1290         libvlc_event_detach(eventManager, libvlc_MediaPlayerEncounteredError,
1291                             handle_input_state_event, this);
1292
1293         libvlc_event_detach(eventManager, libvlc_MediaPlayerTimeChanged,
1294                             handle_time_changed_event, this);
1295         libvlc_event_detach(eventManager, libvlc_MediaPlayerPositionChanged,
1296                             handle_position_changed_event, this);
1297         libvlc_event_detach(eventManager, libvlc_MediaPlayerSeekableChanged,
1298                             handle_seekable_changed_event, this);
1299         libvlc_event_detach(eventManager, libvlc_MediaPlayerPausableChanged,
1300                             handle_pausable_changed_event, this);
1301     }
1302 }
1303
1304 int  VLCPlugin::playlist_add_extended_untrusted(const char *mrl, int optc, const char **optv)
1305 {
1306     int item = -1;
1307     libvlc_media_t *p_m = libvlc_media_new_location(_p_libvlc,mrl);
1308     if( !p_m )
1309         return -1;
1310
1311     for( int i = 0; i < optc; ++i )
1312         libvlc_media_add_option_flag(p_m, optv[i], libvlc_media_option_unique);
1313
1314     libvlc_media_list_lock(_p_mlist);
1315     if( libvlc_media_list_add_media(_p_mlist,p_m) == 0 )
1316         item = libvlc_media_list_count(_p_mlist)-1;
1317     libvlc_media_list_unlock(_p_mlist);
1318     libvlc_media_release(p_m);
1319
1320     return item;
1321 }