]> git.sesse.net Git - vlc/blob - activex/plugin.cpp
ActiveX: Reflect libvlc changes: media_instance destroy_and_detach->release.
[vlc] / activex / plugin.cpp
1 /*****************************************************************************
2  * plugin.cpp: ActiveX control for VLC
3  *****************************************************************************
4  * Copyright (C) 2006 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
25 #include "oleobject.h"
26 #include "olecontrol.h"
27 #include "oleinplaceobject.h"
28 #include "oleinplaceactiveobject.h"
29 #include "persistpropbag.h"
30 #include "persiststreaminit.h"
31 #include "persiststorage.h"
32 #include "provideclassinfo.h"
33 #include "connectioncontainer.h"
34 #include "objectsafety.h"
35 #include "vlccontrol.h"
36 #include "vlccontrol2.h"
37 #include "viewobject.h"
38 #include "dataobject.h"
39 #include "supporterrorinfo.h"
40
41 #include "utils.h"
42
43 #include <string.h>
44 #include <winreg.h>
45 #include <winuser.h>
46 #include <servprov.h>
47 #include <shlwapi.h>
48 #include <wininet.h>
49
50 using namespace std;
51
52 ////////////////////////////////////////////////////////////////////////
53 //class factory
54
55 static LRESULT CALLBACK VLCInPlaceClassWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
56     VLCPlugin *p_instance = reinterpret_cast<VLCPlugin *>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
57
58     switch( uMsg )
59     {
60         case WM_ERASEBKGND:
61             return 1L;
62
63         case WM_PAINT:
64             PAINTSTRUCT ps;
65             RECT pr;
66             if( GetUpdateRect(hWnd, &pr, FALSE) )
67             {
68                 RECT bounds;
69                 GetClientRect(hWnd, &bounds);
70                 BeginPaint(hWnd, &ps);
71                 p_instance->onPaint(ps.hdc, bounds, pr);
72                 EndPaint(hWnd, &ps);
73             }
74             return 0L;
75
76         default:
77             return DefWindowProc(hWnd, uMsg, wParam, lParam);
78     }
79 };
80
81 VLCPluginClass::VLCPluginClass(LONG *p_class_ref, HINSTANCE hInstance, REFCLSID rclsid) :
82     _p_class_ref(p_class_ref),
83     _hinstance(hInstance),
84     _classid(rclsid),
85     _inplace_picture(NULL)
86 {
87     WNDCLASS wClass;
88
89     if( ! GetClassInfo(hInstance, getInPlaceWndClassName(), &wClass) )
90     {
91         wClass.style          = CS_NOCLOSE|CS_DBLCLKS;
92         wClass.lpfnWndProc    = VLCInPlaceClassWndProc;
93         wClass.cbClsExtra     = 0;
94         wClass.cbWndExtra     = 0;
95         wClass.hInstance      = hInstance;
96         wClass.hIcon          = NULL;
97         wClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
98         wClass.hbrBackground  = NULL;
99         wClass.lpszMenuName   = NULL;
100         wClass.lpszClassName  = getInPlaceWndClassName();
101
102         _inplace_wndclass_atom = RegisterClass(&wClass);
103     }
104     else
105     {
106         _inplace_wndclass_atom = 0;
107     }
108
109     HBITMAP hbitmap = (HBITMAP)LoadImage(getHInstance(), TEXT("INPLACE-PICT"), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
110     if( NULL != hbitmap )
111     {
112         PICTDESC pictDesc;
113
114         pictDesc.cbSizeofstruct = sizeof(PICTDESC);
115         pictDesc.picType        = PICTYPE_BITMAP;
116         pictDesc.bmp.hbitmap    = hbitmap;
117         pictDesc.bmp.hpal       = NULL;
118
119         if( FAILED(OleCreatePictureIndirect(&pictDesc, IID_IPicture, TRUE, reinterpret_cast<LPVOID*>(&_inplace_picture))) )
120             _inplace_picture = NULL;
121     }
122     AddRef();
123 };
124
125 VLCPluginClass::~VLCPluginClass()
126 {
127     if( 0 != _inplace_wndclass_atom )
128         UnregisterClass(MAKEINTATOM(_inplace_wndclass_atom), _hinstance);
129
130     if( NULL != _inplace_picture )
131         _inplace_picture->Release();
132 };
133
134 STDMETHODIMP VLCPluginClass::QueryInterface(REFIID riid, void **ppv)
135 {
136     if( NULL == ppv )
137         return E_INVALIDARG;
138
139     if( (IID_IUnknown == riid)
140      || (IID_IClassFactory == riid) )
141     {
142         AddRef();
143         *ppv = reinterpret_cast<LPVOID>(this);
144
145         return NOERROR;
146     }
147
148     *ppv = NULL;
149
150     return E_NOINTERFACE;
151 };
152
153 STDMETHODIMP_(ULONG) VLCPluginClass::AddRef(void)
154 {
155     return InterlockedIncrement(_p_class_ref);
156 };
157
158 STDMETHODIMP_(ULONG) VLCPluginClass::Release(void)
159 {
160     ULONG refcount = InterlockedDecrement(_p_class_ref);
161     if( 0 == refcount )
162     {
163         delete this;
164         return 0;
165     }
166     return refcount;
167 };
168
169 STDMETHODIMP VLCPluginClass::CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, void **ppv)
170 {
171     if( NULL == ppv )
172         return E_POINTER;
173
174     *ppv = NULL;
175
176     if( (NULL != pUnkOuter) && (IID_IUnknown != riid) ) {
177         return CLASS_E_NOAGGREGATION;
178     }
179
180     VLCPlugin *plugin = new VLCPlugin(this, pUnkOuter);
181     if( NULL != plugin )
182     {
183         HRESULT hr = plugin->QueryInterface(riid, ppv);
184         // the following will destroy the object if QueryInterface() failed
185         plugin->Release();
186         return hr;
187     }
188     return E_OUTOFMEMORY;
189 };
190
191 STDMETHODIMP VLCPluginClass::LockServer(BOOL fLock)
192 {
193     if( fLock )
194         AddRef();
195     else 
196         Release();
197
198     return S_OK;
199 };
200
201 ////////////////////////////////////////////////////////////////////////
202
203 VLCPlugin::VLCPlugin(VLCPluginClass *p_class, LPUNKNOWN pUnkOuter) :
204     _inplacewnd(NULL),
205     _p_class(p_class),
206     _i_ref(1UL),
207     _p_libvlc(NULL),
208     _i_codepage(CP_ACP),
209     _b_usermode(TRUE)
210 {
211     p_class->AddRef();
212
213     vlcOleControl = new VLCOleControl(this);
214     vlcOleInPlaceObject = new VLCOleInPlaceObject(this);
215     vlcOleInPlaceActiveObject = new VLCOleInPlaceActiveObject(this);
216     vlcPersistStorage = new VLCPersistStorage(this);
217     vlcPersistStreamInit = new VLCPersistStreamInit(this);
218     vlcPersistPropertyBag = new VLCPersistPropertyBag(this);
219     vlcProvideClassInfo = new VLCProvideClassInfo(this);
220     vlcConnectionPointContainer = new VLCConnectionPointContainer(this);
221     vlcObjectSafety = new VLCObjectSafety(this);
222     vlcControl = new VLCControl(this);
223     vlcControl2 = new VLCControl2(this);
224     vlcViewObject = new VLCViewObject(this);
225     vlcDataObject = new VLCDataObject(this);
226     vlcOleObject = new VLCOleObject(this);
227     vlcSupportErrorInfo = new VLCSupportErrorInfo(this);
228
229     // configure controlling IUnknown interface for implemented interfaces
230     this->pUnkOuter = (NULL != pUnkOuter) ? pUnkOuter : dynamic_cast<LPUNKNOWN>(this);
231
232     // default picure
233     _p_pict = p_class->getInPlacePict();
234
235     // make sure that persistable properties are initialized
236     onInit();
237 };
238
239 VLCPlugin::~VLCPlugin()
240 {
241     delete vlcSupportErrorInfo;
242     delete vlcOleObject;
243     delete vlcDataObject;
244     delete vlcViewObject;
245     delete vlcControl2;
246     delete vlcControl;
247     delete vlcConnectionPointContainer;
248     delete vlcProvideClassInfo;
249     delete vlcPersistPropertyBag;
250     delete vlcPersistStreamInit;
251     delete vlcPersistStorage;
252     delete vlcOleInPlaceActiveObject;
253     delete vlcOleInPlaceObject;
254     delete vlcObjectSafety;
255
256     delete vlcOleControl;
257     if( _p_pict )
258         _p_pict->Release();
259
260     SysFreeString(_bstr_mrl);
261     SysFreeString(_bstr_baseurl);
262
263     _p_class->Release();
264 };
265
266 STDMETHODIMP VLCPlugin::QueryInterface(REFIID riid, void **ppv)
267 {
268     if( NULL == ppv )
269         return E_INVALIDARG;
270
271     if( IID_IUnknown == riid )
272         *ppv = reinterpret_cast<LPVOID>(this);
273     else if( IID_IOleObject == riid )
274         *ppv = reinterpret_cast<LPVOID>(vlcOleObject);
275     else if( IID_IOleControl == riid )
276         *ppv = reinterpret_cast<LPVOID>(vlcOleControl);
277     else if( IID_IOleWindow == riid )
278         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
279     else if( IID_IOleInPlaceObject == riid )
280         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
281     else if( IID_IOleInPlaceActiveObject == riid )
282         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceActiveObject);
283     else if( IID_IPersist == riid )
284         *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
285     else if( IID_IPersistStreamInit == riid )
286         *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
287     else if( IID_IPersistStorage == riid )
288         *ppv = reinterpret_cast<LPVOID>(vlcPersistStorage);
289     else if( IID_IPersistPropertyBag == riid )
290         *ppv = reinterpret_cast<LPVOID>(vlcPersistPropertyBag);
291     else if( IID_IProvideClassInfo == riid )
292         *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
293     else if( IID_IProvideClassInfo2 == riid )
294         *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
295     else if( IID_IConnectionPointContainer == riid )
296         *ppv = reinterpret_cast<LPVOID>(vlcConnectionPointContainer);
297     else if( IID_IObjectSafety == riid )
298         *ppv = reinterpret_cast<LPVOID>(vlcObjectSafety);
299     else if( IID_IDispatch == riid )
300         *ppv = (CLSID_VLCPlugin2 == getClassID()) ?
301                 reinterpret_cast<LPVOID>(vlcControl2) :
302                 reinterpret_cast<LPVOID>(vlcControl);
303     else if( IID_IVLCControl == riid )
304         *ppv = reinterpret_cast<LPVOID>(vlcControl);
305     else if( IID_IVLCControl2 == riid )
306         *ppv = reinterpret_cast<LPVOID>(vlcControl2);
307     else if( IID_IViewObject == riid )
308         *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
309     else if( IID_IViewObject2 == riid )
310         *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
311     else if( IID_IDataObject == riid )
312         *ppv = reinterpret_cast<LPVOID>(vlcDataObject);
313     else if( IID_ISupportErrorInfo == riid )
314         *ppv = reinterpret_cast<LPVOID>(vlcSupportErrorInfo);
315     else
316     {
317         *ppv = NULL;
318         return E_NOINTERFACE;
319     }
320     ((LPUNKNOWN)*ppv)->AddRef();
321     return NOERROR;
322 };
323
324 STDMETHODIMP_(ULONG) VLCPlugin::AddRef(void)
325 {
326     return InterlockedIncrement((LONG *)&_i_ref);
327 };
328
329 STDMETHODIMP_(ULONG) VLCPlugin::Release(void)
330 {
331     if( ! InterlockedDecrement((LONG *)&_i_ref) )
332     {
333         delete this;
334         return 0;
335     }
336     return _i_ref;
337 };
338
339 //////////////////////////////////////
340
341 HRESULT VLCPlugin::onInit(void)
342 {
343     if( NULL == _p_libvlc )
344     {
345         // initialize persistable properties
346         _b_autoplay = TRUE;
347         _b_autoloop = FALSE;
348         _bstr_baseurl = NULL;
349         _bstr_mrl = NULL;
350         _b_visible = TRUE;
351         _b_mute = FALSE;
352         _i_volume = 50;
353         _i_time   = 0;
354         // set default/preferred size (320x240) pixels in HIMETRIC
355         HDC hDC = CreateDevDC(NULL);
356         _extent.cx = 320;
357         _extent.cy = 240;
358         HimetricFromDP(hDC, (LPPOINT)&_extent, 1);
359         DeleteDC(hDC);
360
361         return S_OK;
362     }
363     return CO_E_ALREADYINITIALIZED;
364 };
365
366 HRESULT VLCPlugin::onLoad(void)
367 {
368     if( SysStringLen(_bstr_baseurl) == 0 )
369     {
370         /*
371         ** try to retreive the base URL using the client site moniker, which for Internet Explorer
372         ** is the URL of the page the plugin is embedded into. 
373         */
374         LPOLECLIENTSITE pClientSite;
375         if( SUCCEEDED(vlcOleObject->GetClientSite(&pClientSite)) && (NULL != pClientSite) )
376         {
377             IBindCtx *pBC = 0;
378             if( SUCCEEDED(CreateBindCtx(0, &pBC)) )
379             {
380                 LPMONIKER pContMoniker = NULL;
381                 if( SUCCEEDED(pClientSite->GetMoniker(OLEGETMONIKER_ONLYIFTHERE,
382                                 OLEWHICHMK_CONTAINER, &pContMoniker)) )
383                 {
384                     LPOLESTR base_url;
385                     if( SUCCEEDED(pContMoniker->GetDisplayName(pBC, NULL, &base_url)) )
386                     {
387                         /*
388                         ** check that the moniker name is a URL
389                         */
390                         if( UrlIsW(base_url, URLIS_URL) )
391                         {
392                             /* copy base URL */
393                             _bstr_baseurl = SysAllocString(base_url);
394                         }
395                         CoTaskMemFree(base_url);
396                     }
397                 }
398             }
399         }
400     }
401     setDirty(FALSE);
402     return S_OK;
403 };
404
405 HRESULT VLCPlugin::getVLCObject(int* i_vlc)
406 {
407     libvlc_instance_t *p_libvlc;
408     HRESULT result = getVLC(&p_libvlc);
409     if( SUCCEEDED(result) )
410     {
411         *i_vlc = libvlc_get_vlc_id(p_libvlc);
412     }
413     else
414     {
415         *i_vlc = 0;
416     }
417     return result;
418 }
419
420 HRESULT VLCPlugin::getVLC(libvlc_instance_t** pp_libvlc)
421 {
422     extern HMODULE DllGetModule();
423
424     if( ! isRunning() )
425     {
426         /*
427         ** default initialization options
428         */
429         char *ppsz_argv[32] = { "vlc" };
430         int   ppsz_argc = 1;
431
432         char p_progpath[MAX_PATH];
433         {
434             TCHAR w_progpath[MAX_PATH];
435             DWORD len = GetModuleFileName(DllGetModule(), w_progpath, MAX_PATH);
436             if( len > 0 )
437             {
438                 len = WideCharToMultiByte(CP_UTF8, 0, w_progpath, len, p_progpath,
439                            sizeof(p_progpath)-1, NULL, NULL);
440                 if( len > 0 )
441                 {
442                     p_progpath[len] = '\0';
443                     ppsz_argv[0] = p_progpath;
444                 }
445             }
446         }
447
448         ppsz_argv[ppsz_argc++] = "-vv";
449
450         HKEY h_key;
451         char p_pluginpath[MAX_PATH];
452         if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, TEXT("Software\\VideoLAN\\VLC"),
453                           0, KEY_READ, &h_key ) == ERROR_SUCCESS )
454         {
455             DWORD i_type, i_data = MAX_PATH;
456             TCHAR w_pluginpath[MAX_PATH];
457             if( RegQueryValueEx( h_key, TEXT("InstallDir"), 0, &i_type,
458                                  (LPBYTE)w_pluginpath, &i_data ) == ERROR_SUCCESS )
459             {
460                 if( i_type == REG_SZ )
461                 {
462                     if( WideCharToMultiByte(CP_UTF8, 0, w_pluginpath, -1, p_pluginpath,
463                              sizeof(p_pluginpath)-sizeof("\\plugins")+1, NULL, NULL) )
464                     {
465                         strcat( p_pluginpath, "\\plugins" );
466                         ppsz_argv[ppsz_argc++] = "--plugin-path";
467                         ppsz_argv[ppsz_argc++] = p_pluginpath;
468                     }
469                 }
470             }
471             RegCloseKey( h_key );
472         }
473
474         // make sure plugin isn't affected with VLC single instance mode
475         ppsz_argv[ppsz_argc++] = "--no-one-instance";
476
477         /* common settings */
478         ppsz_argv[ppsz_argc++] = "--no-stats";
479         ppsz_argv[ppsz_argc++] = "--no-media-library";
480         ppsz_argv[ppsz_argc++] = "--intf=dummy";
481
482         // loop mode is a configuration option only
483         if( _b_autoloop )
484             ppsz_argv[ppsz_argc++] = "--loop";
485
486         _p_libvlc = libvlc_new(ppsz_argc, ppsz_argv, NULL);
487         if( NULL == _p_libvlc )
488         {
489             *pp_libvlc = NULL;
490             return E_FAIL;
491         }
492
493         // initial volume setting
494         libvlc_audio_set_volume(_p_libvlc, _i_volume, NULL);
495         if( _b_mute )
496         {
497             libvlc_audio_set_mute(_p_libvlc, TRUE, NULL);
498         }
499
500         // initial playlist item
501         if( SysStringLen(_bstr_mrl) > 0 )
502         {
503             char *psz_mrl = NULL;
504
505             if( SysStringLen(_bstr_baseurl) > 0 )
506             {
507                 /*
508                 ** if the MRL a relative URL, we should end up with an absolute URL
509                 */
510                 LPWSTR abs_url = CombineURL(_bstr_baseurl, _bstr_mrl);
511                 if( NULL != abs_url )
512                 {
513                     psz_mrl = CStrFromWSTR(CP_UTF8, abs_url, wcslen(abs_url));
514                     CoTaskMemFree(abs_url);
515                 }
516                 else
517                 {
518                     psz_mrl = CStrFromBSTR(CP_UTF8, _bstr_mrl);
519                 }
520             }
521             else
522             {
523                 /*
524                 ** baseURL is empty, assume MRL is absolute
525                 */
526                 psz_mrl = CStrFromBSTR(CP_UTF8, _bstr_mrl);
527             }
528             if( NULL != psz_mrl )
529             {
530                 const char *options[1];
531                 int i_options = 0;
532
533                 char timeBuffer[32];
534                 if( _i_time )
535                 {
536                     snprintf(timeBuffer, sizeof(timeBuffer), ":start-time=%d", _i_time);
537                     options[i_options++] = timeBuffer;
538                 }
539                 // add default target to playlist
540                 libvlc_playlist_add_extended(_p_libvlc, psz_mrl, NULL, i_options, options, NULL);
541                 CoTaskMemFree(psz_mrl);
542             }
543         }
544     }
545     *pp_libvlc = _p_libvlc;
546     return S_OK;
547 };
548
549 void VLCPlugin::setErrorInfo(REFIID riid, const char *description)
550 {
551     vlcSupportErrorInfo->setErrorInfo( getClassID() == CLSID_VLCPlugin2 ?
552         OLESTR("VideoLAN.VLCPlugin.2") : OLESTR("VideoLAN.VLCPlugin.1"),
553         riid, description );
554 };
555
556 HRESULT VLCPlugin::onAmbientChanged(LPUNKNOWN pContainer, DISPID dispID)
557 {
558     VARIANT v;
559     switch( dispID )
560     {
561         case DISPID_AMBIENT_BACKCOLOR:
562             break;
563         case DISPID_AMBIENT_DISPLAYNAME:
564             break;
565         case DISPID_AMBIENT_FONT:
566             break;
567         case DISPID_AMBIENT_FORECOLOR:
568             break;
569         case DISPID_AMBIENT_LOCALEID:
570             break;
571         case DISPID_AMBIENT_MESSAGEREFLECT:
572             break;
573         case DISPID_AMBIENT_SCALEUNITS:
574             break;
575         case DISPID_AMBIENT_TEXTALIGN:
576             break;
577         case DISPID_AMBIENT_USERMODE:
578             VariantInit(&v);
579             V_VT(&v) = VT_BOOL;
580             if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
581             {
582                 setUserMode(V_BOOL(&v) != VARIANT_FALSE);
583             }
584             break;
585         case DISPID_AMBIENT_UIDEAD:
586             break;
587         case DISPID_AMBIENT_SHOWGRABHANDLES:
588             break;
589         case DISPID_AMBIENT_SHOWHATCHING:
590             break;
591         case DISPID_AMBIENT_DISPLAYASDEFAULT:
592             break;
593         case DISPID_AMBIENT_SUPPORTSMNEMONICS:
594             break;
595         case DISPID_AMBIENT_AUTOCLIP:
596             break;
597         case DISPID_AMBIENT_APPEARANCE:
598             break;
599         case DISPID_AMBIENT_CODEPAGE:
600             VariantInit(&v);
601             V_VT(&v) = VT_I4;
602             if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
603             {
604                 setCodePage(V_I4(&v));
605             }
606             break;
607         case DISPID_AMBIENT_PALETTE:
608             break;
609         case DISPID_AMBIENT_CHARSET:
610             break;
611         case DISPID_AMBIENT_RIGHTTOLEFT:
612             break;
613         case DISPID_AMBIENT_TOPTOBOTTOM:
614             break;
615         case DISPID_UNKNOWN:
616             /*
617             ** multiple property change, look up the ones we are interested in
618             */
619             VariantInit(&v);
620             V_VT(&v) = VT_BOOL;
621             if( SUCCEEDED(GetObjectProperty(pContainer, DISPID_AMBIENT_USERMODE, v)) )
622             {
623                 setUserMode(V_BOOL(&v) != VARIANT_FALSE);
624             }
625             VariantInit(&v);
626             V_VT(&v) = VT_I4;
627             if( SUCCEEDED(GetObjectProperty(pContainer, DISPID_AMBIENT_CODEPAGE, v)) )
628             {
629                 setCodePage(V_I4(&v));
630             }
631             break;
632     }
633     return S_OK;
634 };
635
636 HRESULT VLCPlugin::onClose(DWORD dwSaveOption)
637 {
638     if( isInPlaceActive() )
639     {
640         onInPlaceDeactivate();
641     }
642     if( isRunning() )
643     {
644         libvlc_instance_t* p_libvlc = _p_libvlc;
645
646         IVLCLog *p_log;
647         if( SUCCEEDED(vlcControl2->get_log(&p_log)) )
648         {
649             // make sure the log is disabled
650             p_log->put_verbosity(-1);
651             p_log->Release();
652         }
653
654         _p_libvlc = NULL;
655         vlcDataObject->onClose();
656
657         libvlc_destroy(p_libvlc, NULL );
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         /* set internal video width and height */
715         libvlc_video_set_size(p_libvlc,
716             lprcPosRect->right-lprcPosRect->left,
717             lprcPosRect->bottom-lprcPosRect->top,
718             NULL );
719
720         /* set internal video parent window */
721         libvlc_video_set_parent(p_libvlc,
722             reinterpret_cast<libvlc_drawable_t>(_inplacewnd), NULL);
723
724         if( _b_autoplay & (libvlc_playlist_items_count(p_libvlc, NULL) > 0) )
725         {
726             libvlc_playlist_play(p_libvlc, 0, 0, NULL, NULL);
727             fireOnPlayEvent();
728         }
729     }
730
731     if( isVisible() )
732         ShowWindow(_inplacewnd, SW_SHOW);
733
734     return S_OK;
735 };
736
737 HRESULT VLCPlugin::onInPlaceDeactivate(void)
738 {
739     if( isRunning() )
740     {
741         libvlc_playlist_stop(_p_libvlc, NULL);
742         fireOnStopEvent();
743     }
744
745     DestroyWindow(_inplacewnd);
746     _inplacewnd = NULL;
747
748     return S_OK;
749 };
750
751 void VLCPlugin::setVisible(BOOL fVisible)
752 {
753     if( fVisible != _b_visible )
754     {
755         _b_visible = fVisible;
756         if( isInPlaceActive() )
757         {
758             ShowWindow(_inplacewnd, fVisible ? SW_SHOW : SW_HIDE);
759             if( fVisible )
760                 InvalidateRect(_inplacewnd, NULL, TRUE);
761         }
762         setDirty(TRUE);
763         firePropChangedEvent(DISPID_Visible);
764     }
765 };
766
767 void VLCPlugin::setVolume(int volume)
768 {
769     if( volume < 0 )
770         volume = 0;
771     else if( volume > 200 )
772         volume = 200;
773
774     if( volume != _i_volume )
775     {
776         _i_volume = volume;
777         if( isRunning() )
778         {
779             libvlc_audio_set_volume(_p_libvlc, _i_volume, NULL);
780         }
781         setDirty(TRUE);
782     }
783 };
784
785 void VLCPlugin::setTime(int seconds)
786 {
787     if( seconds < 0 )
788         seconds = 0;
789
790     if( seconds != _i_time )
791     {
792         setStartTime(_i_time);
793         if( isRunning() )
794         {
795             libvlc_media_instance_t *p_md = libvlc_playlist_get_media_instance(_p_libvlc, NULL);
796             if( NULL != p_md )
797             {
798                 libvlc_media_instance_set_time(p_md, _i_time, NULL);
799                 libvlc_media_instance_release(p_md);
800             }
801         }
802     }
803 };
804
805 void VLCPlugin::setFocus(BOOL fFocus)
806 {
807     if( fFocus )
808         SetActiveWindow(_inplacewnd);
809 };
810
811 BOOL VLCPlugin::hasFocus(void)
812 {
813     return GetActiveWindow() == _inplacewnd;
814 };
815
816 void VLCPlugin::onDraw(DVTARGETDEVICE * ptd, HDC hicTargetDev,
817         HDC hdcDraw, LPCRECTL lprcBounds, LPCRECTL lprcWBounds)
818 {
819     if( isVisible() )
820     {
821         long width = lprcBounds->right-lprcBounds->left;
822         long height = lprcBounds->bottom-lprcBounds->top;
823
824         RECT bounds = { lprcBounds->left, lprcBounds->top, lprcBounds->right, lprcBounds->bottom };
825         FillRect(hdcDraw, &bounds, (HBRUSH)GetStockObject(WHITE_BRUSH));
826
827         LPPICTURE pict = getPicture();
828         if( NULL != pict )
829         {
830             OLE_XSIZE_HIMETRIC picWidth;
831             OLE_YSIZE_HIMETRIC picHeight;
832
833             pict->get_Width(&picWidth);
834             pict->get_Height(&picHeight);
835
836             SIZEL picSize = { picWidth, picHeight };
837
838             if( NULL != hicTargetDev )
839             {
840                 DPFromHimetric(hicTargetDev, (LPPOINT)&picSize, 1);
841             }
842             else if( NULL != (hicTargetDev = CreateDevDC(ptd)) )
843             {
844                 DPFromHimetric(hicTargetDev, (LPPOINT)&picSize, 1);
845                 DeleteDC(hicTargetDev);
846             }
847
848             if( picSize.cx > width-4 )
849                 picSize.cx = width-4;
850             if( picSize.cy > height-4 )
851                 picSize.cy = height-4;
852
853             LONG dstX = lprcBounds->left+(width-picSize.cx)/2;
854             LONG dstY = lprcBounds->top+(height-picSize.cy)/2;
855
856             if( NULL != lprcWBounds )
857             {
858                 RECT wBounds = { lprcWBounds->left, lprcWBounds->top, lprcWBounds->right, lprcWBounds->bottom };
859                 pict->Render(hdcDraw, dstX, dstY, picSize.cx, picSize.cy,
860                         0L, picHeight, picWidth, -picHeight, &wBounds);
861             }
862             else 
863                 pict->Render(hdcDraw, dstX, dstY, picSize.cx, picSize.cy,
864                         0L, picHeight, picWidth, -picHeight, NULL);
865
866             pict->Release();
867         }
868
869         SelectObject(hdcDraw, GetStockObject(BLACK_BRUSH));
870
871         MoveToEx(hdcDraw, bounds.left, bounds.top, NULL);
872         LineTo(hdcDraw, bounds.left+width-1, bounds.top);
873         LineTo(hdcDraw, bounds.left+width-1, bounds.top+height-1);
874         LineTo(hdcDraw, bounds.left, bounds.top+height-1);
875         LineTo(hdcDraw, bounds.left, bounds.top);
876     }
877 };
878
879 void VLCPlugin::onPaint(HDC hdc, const RECT &bounds, const RECT &clipRect)
880 {
881     if( isVisible() )
882     {
883         /** if VLC is playing, it may not display any VIDEO content 
884         ** hence, draw control logo*/
885         HDC hdcDraw = CreateCompatibleDC(hdc);
886         if( NULL != hdcDraw )
887         {
888             SIZEL size = getExtent();
889             DPFromHimetric(hdc, (LPPOINT)&size, 1);
890             RECTL posRect = { 0, 0, size.cx, size.cy };
891
892             int width = bounds.right-bounds.left;
893             int height = bounds.bottom-bounds.top;
894
895             HBITMAP hBitmap = CreateCompatibleBitmap(hdc, width, height);
896             if( NULL != hBitmap )
897             {
898                 HBITMAP oldBmp = (HBITMAP)SelectObject(hdcDraw, hBitmap);
899
900                 if( (size.cx != width) || (size.cy != height) )
901                 {
902                     // needs to scale canvas
903                     SetMapMode(hdcDraw, MM_ANISOTROPIC);
904                     SetWindowExtEx(hdcDraw, size.cx, size.cy, NULL);
905                     SetViewportExtEx(hdcDraw, width, height, NULL);
906                 }
907
908                 onDraw(NULL, hdc, hdcDraw, &posRect, NULL);
909
910                 SetMapMode(hdcDraw, MM_TEXT);
911                 BitBlt(hdc, bounds.left, bounds.top,
912                         width, height,
913                         hdcDraw, 0, 0,
914                         SRCCOPY);
915
916                 SelectObject(hdcDraw, oldBmp);
917                 DeleteObject(hBitmap);
918             }
919             DeleteDC(hdcDraw);
920         }
921     }
922 };
923
924 void VLCPlugin::onPositionChange(LPCRECT lprcPosRect, LPCRECT lprcClipRect)
925 {
926     RECT clipRect = *lprcClipRect;
927
928     //RedrawWindow(GetParent(_inplacewnd), &_posRect, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
929
930     /*
931     ** record keeping of control geometry within container
932     */
933     _posRect = *lprcPosRect;
934
935     /*
936     ** change in-place window geometry to match clipping region
937     */
938     SetWindowPos(_inplacewnd, NULL,
939             lprcPosRect->left,
940             lprcPosRect->top,
941             lprcPosRect->right-lprcPosRect->left,
942             lprcPosRect->bottom-lprcPosRect->top,
943             SWP_NOACTIVATE|
944             SWP_NOCOPYBITS|
945             SWP_NOZORDER|
946             SWP_NOOWNERZORDER );
947
948     /* change cliprect coordinates system relative to window bounding rect */
949     OffsetRect(&clipRect, -lprcPosRect->left, -lprcPosRect->top);
950     HRGN clipRgn = CreateRectRgnIndirect(&clipRect);
951     SetWindowRgn(_inplacewnd, clipRgn, FALSE);
952
953     //RedrawWindow(_videownd, &posRect, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
954     if( isRunning() )
955     {
956         libvlc_video_set_size(_p_libvlc,
957             lprcPosRect->right-lprcPosRect->left,
958             lprcPosRect->bottom-lprcPosRect->top,
959             NULL );
960     }
961 };
962
963 void VLCPlugin::freezeEvents(BOOL freeze)
964 {
965     vlcConnectionPointContainer->freezeEvents(freeze);
966 };
967
968 void VLCPlugin::firePropChangedEvent(DISPID dispid)
969 {
970     vlcConnectionPointContainer->firePropChangedEvent(dispid); 
971 };
972
973 void VLCPlugin::fireOnPlayEvent(void)
974 {
975     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
976     vlcConnectionPointContainer->fireEvent(DISPID_PlayEvent, &dispparamsNoArgs); 
977 };
978
979 void VLCPlugin::fireOnPauseEvent(void)
980 {
981     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
982     vlcConnectionPointContainer->fireEvent(DISPID_PauseEvent, &dispparamsNoArgs); 
983 };
984
985 void VLCPlugin::fireOnStopEvent(void)
986 {
987     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
988     vlcConnectionPointContainer->fireEvent(DISPID_StopEvent, &dispparamsNoArgs); 
989 };