]> git.sesse.net Git - vlc/blob - projects/activex/plugin.cpp
Merge branch 'master' of git@git.videolan.org:vlc
[vlc] / projects / 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(), MAKEINTRESOURCE(2), 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     /*
242     ** bump refcount to avoid recursive release from
243     ** following interfaces when releasing this interface
244     */
245     AddRef();
246
247     delete vlcSupportErrorInfo;
248     delete vlcOleObject;
249     delete vlcDataObject;
250     delete vlcViewObject;
251     delete vlcControl2;
252     delete vlcControl;
253     delete vlcConnectionPointContainer;
254     delete vlcProvideClassInfo;
255     delete vlcPersistPropertyBag;
256     delete vlcPersistStreamInit;
257     delete vlcPersistStorage;
258     delete vlcOleInPlaceActiveObject;
259     delete vlcOleInPlaceObject;
260     delete vlcObjectSafety;
261
262     delete vlcOleControl;
263     if( _p_pict )
264         _p_pict->Release();
265
266     SysFreeString(_bstr_mrl);
267     SysFreeString(_bstr_baseurl);
268
269     _p_class->Release();
270 };
271
272 STDMETHODIMP VLCPlugin::QueryInterface(REFIID riid, void **ppv)
273 {
274     if( NULL == ppv )
275         return E_INVALIDARG;
276
277     if( IID_IUnknown == riid )
278         *ppv = reinterpret_cast<LPVOID>(this);
279     else if( IID_IOleObject == riid )
280         *ppv = reinterpret_cast<LPVOID>(vlcOleObject);
281     else if( IID_IOleControl == riid )
282         *ppv = reinterpret_cast<LPVOID>(vlcOleControl);
283     else if( IID_IOleWindow == riid )
284         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
285     else if( IID_IOleInPlaceObject == riid )
286         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
287     else if( IID_IOleInPlaceActiveObject == riid )
288         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceActiveObject);
289     else if( IID_IPersist == riid )
290         *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
291     else if( IID_IPersistStreamInit == riid )
292         *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
293     else if( IID_IPersistStorage == riid )
294         *ppv = reinterpret_cast<LPVOID>(vlcPersistStorage);
295     else if( IID_IPersistPropertyBag == riid )
296         *ppv = reinterpret_cast<LPVOID>(vlcPersistPropertyBag);
297     else if( IID_IProvideClassInfo == riid )
298         *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
299     else if( IID_IProvideClassInfo2 == riid )
300         *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
301     else if( IID_IConnectionPointContainer == riid )
302         *ppv = reinterpret_cast<LPVOID>(vlcConnectionPointContainer);
303     else if( IID_IObjectSafety == riid )
304         *ppv = reinterpret_cast<LPVOID>(vlcObjectSafety);
305     else if( IID_IDispatch == riid )
306         *ppv = (CLSID_VLCPlugin2 == getClassID()) ?
307                 reinterpret_cast<LPVOID>(vlcControl2) :
308                 reinterpret_cast<LPVOID>(vlcControl);
309     else if( IID_IVLCControl == riid )
310         *ppv = reinterpret_cast<LPVOID>(vlcControl);
311     else if( IID_IVLCControl2 == riid )
312         *ppv = reinterpret_cast<LPVOID>(vlcControl2);
313     else if( IID_IViewObject == riid )
314         *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
315     else if( IID_IViewObject2 == riid )
316         *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
317     else if( IID_IDataObject == riid )
318         *ppv = reinterpret_cast<LPVOID>(vlcDataObject);
319     else if( IID_ISupportErrorInfo == riid )
320         *ppv = reinterpret_cast<LPVOID>(vlcSupportErrorInfo);
321     else
322     {
323         *ppv = NULL;
324         return E_NOINTERFACE;
325     }
326     ((LPUNKNOWN)*ppv)->AddRef();
327     return NOERROR;
328 };
329
330 STDMETHODIMP_(ULONG) VLCPlugin::AddRef(void)
331 {
332     return InterlockedIncrement((LONG *)&_i_ref);
333 };
334
335 STDMETHODIMP_(ULONG) VLCPlugin::Release(void)
336 {
337     if( ! InterlockedDecrement((LONG *)&_i_ref) )
338     {
339         delete this;
340         return 0;
341     }
342     return _i_ref;
343 };
344
345 //////////////////////////////////////
346
347 HRESULT VLCPlugin::onInit(void)
348 {
349     if( NULL == _p_libvlc )
350     {
351         // initialize persistable properties
352         _b_autoplay   = TRUE;
353         _b_autoloop   = FALSE;
354         _b_toolbar    = FALSE;
355         _bstr_baseurl = NULL;
356         _bstr_mrl     = NULL;
357         _b_visible    = TRUE;
358         _b_mute       = FALSE;
359         _i_volume     = 50;
360         _i_time       = 0;
361         _i_backcolor  = 0;
362         // set default/preferred size (320x240) pixels in HIMETRIC
363         HDC hDC = CreateDevDC(NULL);
364         _extent.cx = 320;
365         _extent.cy = 240;
366         HimetricFromDP(hDC, (LPPOINT)&_extent, 1);
367         DeleteDC(hDC);
368
369         return S_OK;
370     }
371     return CO_E_ALREADYINITIALIZED;
372 };
373
374 HRESULT VLCPlugin::onLoad(void)
375 {
376     if( SysStringLen(_bstr_baseurl) == 0 )
377     {
378         /*
379         ** try to retreive the base URL using the client site moniker, which for Internet Explorer
380         ** is the URL of the page the plugin is embedded into.
381         */
382         LPOLECLIENTSITE pClientSite;
383         if( SUCCEEDED(vlcOleObject->GetClientSite(&pClientSite)) && (NULL != pClientSite) )
384         {
385             IBindCtx *pBC = 0;
386             if( SUCCEEDED(CreateBindCtx(0, &pBC)) )
387             {
388                 LPMONIKER pContMoniker = NULL;
389                 if( SUCCEEDED(pClientSite->GetMoniker(OLEGETMONIKER_ONLYIFTHERE,
390                                 OLEWHICHMK_CONTAINER, &pContMoniker)) )
391                 {
392                     LPOLESTR base_url;
393                     if( SUCCEEDED(pContMoniker->GetDisplayName(pBC, NULL, &base_url)) )
394                     {
395                         /*
396                         ** check that the moniker name is a URL
397                         */
398                         if( UrlIsW(base_url, URLIS_URL) )
399                         {
400                             /* copy base URL */
401                             _bstr_baseurl = SysAllocString(base_url);
402                         }
403                         CoTaskMemFree(base_url);
404                     }
405                 }
406             }
407         }
408     }
409     setDirty(FALSE);
410     return S_OK;
411 };
412
413 HRESULT VLCPlugin::getVLCObject(int* i_vlc)
414 {
415     libvlc_instance_t *p_libvlc;
416     HRESULT result = getVLC(&p_libvlc);
417     if( SUCCEEDED(result) )
418     {
419         *i_vlc = libvlc_get_vlc_id(p_libvlc);
420     }
421     else
422     {
423         *i_vlc = 0;
424     }
425     return result;
426 }
427
428 HRESULT VLCPlugin::getVLC(libvlc_instance_t** pp_libvlc)
429 {
430     extern HMODULE DllGetModule();
431
432     if( ! isRunning() )
433     {
434         /*
435         ** default initialization options
436         */
437         const char *ppsz_argv[32] = { };
438         int   ppsz_argc = 0;
439
440         char p_progpath[MAX_PATH];
441         {
442             TCHAR w_progpath[MAX_PATH];
443             DWORD len = GetModuleFileName(DllGetModule(), w_progpath, MAX_PATH);
444             if( len > 0 )
445             {
446                 len = WideCharToMultiByte(CP_UTF8, 0, w_progpath, len, p_progpath,
447                            sizeof(p_progpath)-1, NULL, NULL);
448                 if( len > 0 )
449                 {
450                     p_progpath[len] = '\0';
451                     ppsz_argv[0] = p_progpath;
452                 }
453             }
454         }
455
456         ppsz_argv[ppsz_argc++] = "-vv";
457
458         HKEY h_key;
459         char p_pluginpath[MAX_PATH];
460         if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, TEXT("Software\\VideoLAN\\VLC"),
461                           0, KEY_READ, &h_key ) == ERROR_SUCCESS )
462         {
463             DWORD i_type, i_data = MAX_PATH;
464             TCHAR w_pluginpath[MAX_PATH];
465             if( RegQueryValueEx( h_key, TEXT("InstallDir"), 0, &i_type,
466                                  (LPBYTE)w_pluginpath, &i_data ) == ERROR_SUCCESS )
467             {
468                 if( i_type == REG_SZ )
469                 {
470                     if( WideCharToMultiByte(CP_UTF8, 0, w_pluginpath, -1, p_pluginpath,
471                              sizeof(p_pluginpath)-sizeof("\\plugins")+1, NULL, NULL) )
472                     {
473                         strcat( p_pluginpath, "\\plugins" );
474                         ppsz_argv[ppsz_argc++] = "--plugin-path";
475                         ppsz_argv[ppsz_argc++] = p_pluginpath;
476                     }
477                 }
478             }
479             RegCloseKey( h_key );
480         }
481
482         // make sure plugin isn't affected with VLC single instance mode
483         ppsz_argv[ppsz_argc++] = "--no-one-instance";
484
485         /* common settings */
486         ppsz_argv[ppsz_argc++] = "--no-stats";
487         ppsz_argv[ppsz_argc++] = "--no-media-library";
488         ppsz_argv[ppsz_argc++] = "--intf=dummy";
489
490         // loop mode is a configuration option only
491         if( _b_autoloop )
492             ppsz_argv[ppsz_argc++] = "--loop";
493
494         _p_libvlc = libvlc_new(ppsz_argc, ppsz_argv, NULL);
495         if( NULL == _p_libvlc )
496         {
497             *pp_libvlc = NULL;
498             return E_FAIL;
499         }
500
501         // initial volume setting
502         libvlc_audio_set_volume(_p_libvlc, _i_volume, NULL);
503         if( _b_mute )
504         {
505             libvlc_audio_set_mute(_p_libvlc, TRUE, NULL);
506         }
507
508         // initial playlist item
509         if( SysStringLen(_bstr_mrl) > 0 )
510         {
511             char *psz_mrl = NULL;
512
513             if( SysStringLen(_bstr_baseurl) > 0 )
514             {
515                 /*
516                 ** if the MRL a relative URL, we should end up with an absolute URL
517                 */
518                 LPWSTR abs_url = CombineURL(_bstr_baseurl, _bstr_mrl);
519                 if( NULL != abs_url )
520                 {
521                     psz_mrl = CStrFromWSTR(CP_UTF8, abs_url, wcslen(abs_url));
522                     CoTaskMemFree(abs_url);
523                 }
524                 else
525                 {
526                     psz_mrl = CStrFromBSTR(CP_UTF8, _bstr_mrl);
527                 }
528             }
529             else
530             {
531                 /*
532                 ** baseURL is empty, assume MRL is absolute
533                 */
534                 psz_mrl = CStrFromBSTR(CP_UTF8, _bstr_mrl);
535             }
536             if( NULL != psz_mrl )
537             {
538                 const char *options[1];
539                 int i_options = 0;
540
541                 char timeBuffer[32];
542                 if( _i_time )
543                 {
544                     snprintf(timeBuffer, sizeof(timeBuffer), ":start-time=%d", _i_time);
545                     options[i_options++] = timeBuffer;
546                 }
547                 // add default target to playlist
548                 libvlc_playlist_add_extended(_p_libvlc, psz_mrl, NULL, i_options, options, NULL);
549                 CoTaskMemFree(psz_mrl);
550             }
551         }
552     }
553     *pp_libvlc = _p_libvlc;
554     return S_OK;
555 };
556
557 void VLCPlugin::setErrorInfo(REFIID riid, const char *description)
558 {
559     vlcSupportErrorInfo->setErrorInfo( getClassID() == CLSID_VLCPlugin2 ?
560         OLESTR("VideoLAN.VLCPlugin.2") : OLESTR("VideoLAN.VLCPlugin.1"),
561         riid, description );
562 };
563
564 HRESULT VLCPlugin::onAmbientChanged(LPUNKNOWN pContainer, DISPID dispID)
565 {
566     VARIANT v;
567     switch( dispID )
568     {
569         case DISPID_AMBIENT_BACKCOLOR:
570             VariantInit(&v);
571             V_VT(&v) = VT_I4;
572             if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
573             {
574                 setBackColor(V_I4(&v));
575             }
576             break;
577         case DISPID_AMBIENT_DISPLAYNAME:
578             break;
579         case DISPID_AMBIENT_FONT:
580             break;
581         case DISPID_AMBIENT_FORECOLOR:
582             break;
583         case DISPID_AMBIENT_LOCALEID:
584             break;
585         case DISPID_AMBIENT_MESSAGEREFLECT:
586             break;
587         case DISPID_AMBIENT_SCALEUNITS:
588             break;
589         case DISPID_AMBIENT_TEXTALIGN:
590             break;
591         case DISPID_AMBIENT_USERMODE:
592             VariantInit(&v);
593             V_VT(&v) = VT_BOOL;
594             if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
595             {
596                 setUserMode(V_BOOL(&v) != VARIANT_FALSE);
597             }
598             break;
599         case DISPID_AMBIENT_UIDEAD:
600             break;
601         case DISPID_AMBIENT_SHOWGRABHANDLES:
602             break;
603         case DISPID_AMBIENT_SHOWHATCHING:
604             break;
605         case DISPID_AMBIENT_DISPLAYASDEFAULT:
606             break;
607         case DISPID_AMBIENT_SUPPORTSMNEMONICS:
608             break;
609         case DISPID_AMBIENT_AUTOCLIP:
610             break;
611         case DISPID_AMBIENT_APPEARANCE:
612             break;
613         case DISPID_AMBIENT_CODEPAGE:
614             VariantInit(&v);
615             V_VT(&v) = VT_I4;
616             if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
617             {
618                 setCodePage(V_I4(&v));
619             }
620             break;
621         case DISPID_AMBIENT_PALETTE:
622             break;
623         case DISPID_AMBIENT_CHARSET:
624             break;
625         case DISPID_AMBIENT_RIGHTTOLEFT:
626             break;
627         case DISPID_AMBIENT_TOPTOBOTTOM:
628             break;
629         case DISPID_UNKNOWN:
630             /*
631             ** multiple property change, look up the ones we are interested in
632             */
633             VariantInit(&v);
634             V_VT(&v) = VT_BOOL;
635             if( SUCCEEDED(GetObjectProperty(pContainer, DISPID_AMBIENT_USERMODE, v)) )
636             {
637                 setUserMode(V_BOOL(&v) != VARIANT_FALSE);
638             }
639             VariantInit(&v);
640             V_VT(&v) = VT_I4;
641             if( SUCCEEDED(GetObjectProperty(pContainer, DISPID_AMBIENT_CODEPAGE, v)) )
642             {
643                 setCodePage(V_I4(&v));
644             }
645             break;
646     }
647     return S_OK;
648 };
649
650 HRESULT VLCPlugin::onClose(DWORD dwSaveOption)
651 {
652     if( isInPlaceActive() )
653     {
654         onInPlaceDeactivate();
655     }
656     if( isRunning() )
657     {
658         libvlc_instance_t* p_libvlc = _p_libvlc;
659
660         IVLCLog *p_log;
661         if( SUCCEEDED(vlcControl2->get_log(&p_log)) )
662         {
663             // make sure the log is disabled
664             p_log->put_verbosity(-1);
665             p_log->Release();
666         }
667
668         _p_libvlc = NULL;
669         vlcDataObject->onClose();
670
671         libvlc_release(p_libvlc);
672     }
673     return S_OK;
674 };
675
676 BOOL VLCPlugin::isInPlaceActive(void)
677 {
678     return (NULL != _inplacewnd);
679 };
680
681 HRESULT VLCPlugin::onActivateInPlace(LPMSG lpMesg, HWND hwndParent, LPCRECT lprcPosRect, LPCRECT lprcClipRect)
682 {
683     RECT clipRect = *lprcClipRect;
684
685     /*
686     ** record keeping of control geometry within container
687     */
688     _posRect = *lprcPosRect;
689
690     /*
691     ** Create a window for in place activated control.
692     ** the window geometry matches the control viewport
693     ** within container so that embedded video is always
694     ** properly displayed.
695     */
696     _inplacewnd = CreateWindow(_p_class->getInPlaceWndClassName(),
697             TEXT("VLC Plugin In-Place Window"),
698             WS_CHILD|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,
699             lprcPosRect->left,
700             lprcPosRect->top,
701             lprcPosRect->right-lprcPosRect->left,
702             lprcPosRect->bottom-lprcPosRect->top,
703             hwndParent,
704             0,
705             _p_class->getHInstance(),
706             NULL
707            );
708
709     if( NULL == _inplacewnd )
710         return E_FAIL;
711
712     SetWindowLongPtr(_inplacewnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
713
714     /* change cliprect coordinates system relative to window bounding rect */
715     OffsetRect(&clipRect, -lprcPosRect->left, -lprcPosRect->top);
716
717     HRGN clipRgn = CreateRectRgnIndirect(&clipRect);
718     SetWindowRgn(_inplacewnd, clipRgn, TRUE);
719
720     if( _b_usermode )
721     {
722         /* will run vlc if not done already */
723         libvlc_instance_t* p_libvlc;
724         HRESULT result = getVLC(&p_libvlc);
725         if( FAILED(result) )
726             return result;
727
728         /* set internal video width and height */
729         libvlc_video_set_size(p_libvlc,
730             lprcPosRect->right-lprcPosRect->left,
731             lprcPosRect->bottom-lprcPosRect->top,
732             NULL );
733
734         /* set internal video parent window */
735         libvlc_video_set_parent(p_libvlc,
736             reinterpret_cast<libvlc_drawable_t>(_inplacewnd), NULL);
737
738         if( _b_autoplay & (libvlc_playlist_items_count(p_libvlc, NULL) > 0) )
739         {
740             libvlc_playlist_play(p_libvlc, 0, 0, NULL, NULL);
741             fireOnPlayEvent();
742         }
743     }
744
745     if( isVisible() )
746         ShowWindow(_inplacewnd, SW_SHOW);
747
748     return S_OK;
749 };
750
751 HRESULT VLCPlugin::onInPlaceDeactivate(void)
752 {
753     if( isRunning() )
754     {
755         libvlc_playlist_stop(_p_libvlc, NULL);
756         fireOnStopEvent();
757     }
758
759     DestroyWindow(_inplacewnd);
760     _inplacewnd = NULL;
761
762     return S_OK;
763 };
764
765 void VLCPlugin::setVisible(BOOL fVisible)
766 {
767     if( fVisible != _b_visible )
768     {
769         _b_visible = fVisible;
770         if( isInPlaceActive() )
771         {
772             ShowWindow(_inplacewnd, fVisible ? SW_SHOW : SW_HIDE);
773             if( fVisible )
774                 InvalidateRect(_inplacewnd, NULL, TRUE);
775         }
776         setDirty(TRUE);
777         firePropChangedEvent(DISPID_Visible);
778     }
779 };
780
781 void VLCPlugin::setVolume(int volume)
782 {
783     if( volume < 0 )
784         volume = 0;
785     else if( volume > 200 )
786         volume = 200;
787
788     if( volume != _i_volume )
789     {
790         _i_volume = volume;
791         if( isRunning() )
792         {
793             libvlc_audio_set_volume(_p_libvlc, _i_volume, NULL);
794         }
795         setDirty(TRUE);
796     }
797 };
798
799 void VLCPlugin::setBackColor(OLE_COLOR backcolor)
800 {
801     if( _i_backcolor != backcolor )
802     {
803         _i_backcolor = backcolor;
804         if( isInPlaceActive() )
805         {
806
807         }
808         setDirty(TRUE);
809     }
810 };
811
812 void VLCPlugin::setTime(int seconds)
813 {
814     if( seconds < 0 )
815         seconds = 0;
816
817     if( seconds != _i_time )
818     {
819         setStartTime(_i_time);
820         if( isRunning() )
821         {
822             libvlc_media_player_t *p_md = libvlc_playlist_get_media_player(_p_libvlc, NULL);
823             if( NULL != p_md )
824             {
825                 libvlc_media_player_set_time(p_md, _i_time, NULL);
826                 libvlc_media_player_release(p_md);
827             }
828         }
829     }
830 };
831
832 void VLCPlugin::setFocus(BOOL fFocus)
833 {
834     if( fFocus )
835         SetActiveWindow(_inplacewnd);
836 };
837
838 BOOL VLCPlugin::hasFocus(void)
839 {
840     return GetActiveWindow() == _inplacewnd;
841 };
842
843 void VLCPlugin::onDraw(DVTARGETDEVICE * ptd, HDC hicTargetDev,
844         HDC hdcDraw, LPCRECTL lprcBounds, LPCRECTL lprcWBounds)
845 {
846     if( isVisible() )
847     {
848         long width = lprcBounds->right-lprcBounds->left;
849         long height = lprcBounds->bottom-lprcBounds->top;
850
851         RECT bounds = { lprcBounds->left, lprcBounds->top, lprcBounds->right, lprcBounds->bottom };
852
853         if( isUserMode() )
854         {
855             /* VLC is in user mode, just draw background color */
856             COLORREF colorref = RGB(0, 0, 0);
857             OleTranslateColor(_i_backcolor, (HPALETTE)GetStockObject(DEFAULT_PALETTE), &colorref);
858             if( colorref != RGB(0, 0, 0) )
859             {
860                 /* custom background */
861                 HBRUSH colorbrush = CreateSolidBrush(colorref);
862                 FillRect(hdcDraw, &bounds, colorbrush);
863                 DeleteObject((HANDLE)colorbrush);
864             }
865             else
866             {
867                 /* black background */
868                 FillRect(hdcDraw, &bounds, (HBRUSH)GetStockObject(BLACK_BRUSH));
869             }
870         }
871         else
872         {
873             /* VLC is in design mode, draw the VLC logo */
874             FillRect(hdcDraw, &bounds, (HBRUSH)GetStockObject(WHITE_BRUSH));
875
876             LPPICTURE pict = getPicture();
877             if( NULL != pict )
878             {
879                 OLE_XSIZE_HIMETRIC picWidth;
880                 OLE_YSIZE_HIMETRIC picHeight;
881
882                 pict->get_Width(&picWidth);
883                 pict->get_Height(&picHeight);
884
885                 SIZEL picSize = { picWidth, picHeight };
886
887                 if( NULL != hicTargetDev )
888                 {
889                     DPFromHimetric(hicTargetDev, (LPPOINT)&picSize, 1);
890                 }
891                 else if( NULL != (hicTargetDev = CreateDevDC(ptd)) )
892                 {
893                     DPFromHimetric(hicTargetDev, (LPPOINT)&picSize, 1);
894                     DeleteDC(hicTargetDev);
895                 }
896
897                 if( picSize.cx > width-4 )
898                     picSize.cx = width-4;
899                 if( picSize.cy > height-4 )
900                     picSize.cy = height-4;
901
902                 LONG dstX = lprcBounds->left+(width-picSize.cx)/2;
903                 LONG dstY = lprcBounds->top+(height-picSize.cy)/2;
904
905                 if( NULL != lprcWBounds )
906                 {
907                     RECT wBounds = { lprcWBounds->left, lprcWBounds->top, lprcWBounds->right, lprcWBounds->bottom };
908                     pict->Render(hdcDraw, dstX, dstY, picSize.cx, picSize.cy,
909                             0L, picHeight, picWidth, -picHeight, &wBounds);
910                 }
911                 else
912                     pict->Render(hdcDraw, dstX, dstY, picSize.cx, picSize.cy,
913                             0L, picHeight, picWidth, -picHeight, NULL);
914
915                 pict->Release();
916             }
917
918             SelectObject(hdcDraw, GetStockObject(BLACK_BRUSH));
919
920             MoveToEx(hdcDraw, bounds.left, bounds.top, NULL);
921             LineTo(hdcDraw, bounds.left+width-1, bounds.top);
922             LineTo(hdcDraw, bounds.left+width-1, bounds.top+height-1);
923             LineTo(hdcDraw, bounds.left, bounds.top+height-1);
924             LineTo(hdcDraw, bounds.left, bounds.top);
925         }
926     }
927 };
928
929 void VLCPlugin::onPaint(HDC hdc, const RECT &bounds, const RECT &clipRect)
930 {
931     if( isVisible() )
932     {
933         /* if VLC is in design mode, draw control logo */
934         HDC hdcDraw = CreateCompatibleDC(hdc);
935         if( NULL != hdcDraw )
936         {
937             SIZEL size = getExtent();
938             DPFromHimetric(hdc, (LPPOINT)&size, 1);
939             RECTL posRect = { 0, 0, size.cx, size.cy };
940
941             int width = bounds.right-bounds.left;
942             int height = bounds.bottom-bounds.top;
943
944             HBITMAP hBitmap = CreateCompatibleBitmap(hdc, width, height);
945             if( NULL != hBitmap )
946             {
947                 HBITMAP oldBmp = (HBITMAP)SelectObject(hdcDraw, hBitmap);
948
949                 if( (size.cx != width) || (size.cy != height) )
950                 {
951                     // needs to scale canvas
952                     SetMapMode(hdcDraw, MM_ANISOTROPIC);
953                     SetWindowExtEx(hdcDraw, size.cx, size.cy, NULL);
954                     SetViewportExtEx(hdcDraw, width, height, NULL);
955                 }
956
957                 onDraw(NULL, hdc, hdcDraw, &posRect, NULL);
958
959                 SetMapMode(hdcDraw, MM_TEXT);
960                 BitBlt(hdc, bounds.left, bounds.top,
961                         width, height,
962                         hdcDraw, 0, 0,
963                         SRCCOPY);
964
965                 SelectObject(hdcDraw, oldBmp);
966                 DeleteObject(hBitmap);
967             }
968             DeleteDC(hdcDraw);
969         }
970     }
971 };
972
973 void VLCPlugin::onPositionChange(LPCRECT lprcPosRect, LPCRECT lprcClipRect)
974 {
975     RECT clipRect = *lprcClipRect;
976
977     //RedrawWindow(GetParent(_inplacewnd), &_posRect, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
978
979     /*
980     ** record keeping of control geometry within container
981     */
982     _posRect = *lprcPosRect;
983
984     /*
985     ** change in-place window geometry to match clipping region
986     */
987     SetWindowPos(_inplacewnd, NULL,
988             lprcPosRect->left,
989             lprcPosRect->top,
990             lprcPosRect->right-lprcPosRect->left,
991             lprcPosRect->bottom-lprcPosRect->top,
992             SWP_NOACTIVATE|
993             SWP_NOCOPYBITS|
994             SWP_NOZORDER|
995             SWP_NOOWNERZORDER );
996
997     /* change cliprect coordinates system relative to window bounding rect */
998     OffsetRect(&clipRect, -lprcPosRect->left, -lprcPosRect->top);
999     HRGN clipRgn = CreateRectRgnIndirect(&clipRect);
1000     SetWindowRgn(_inplacewnd, clipRgn, FALSE);
1001
1002     //RedrawWindow(_videownd, &posRect, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
1003     if( isRunning() )
1004     {
1005         libvlc_video_set_size(_p_libvlc,
1006             lprcPosRect->right-lprcPosRect->left,
1007             lprcPosRect->bottom-lprcPosRect->top,
1008             NULL );
1009     }
1010 };
1011
1012 void VLCPlugin::freezeEvents(BOOL freeze)
1013 {
1014     vlcConnectionPointContainer->freezeEvents(freeze);
1015 };
1016
1017 void VLCPlugin::firePropChangedEvent(DISPID dispid)
1018 {
1019     vlcConnectionPointContainer->firePropChangedEvent(dispid);
1020 };
1021
1022 void VLCPlugin::fireOnPlayEvent(void)
1023 {
1024     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1025     vlcConnectionPointContainer->fireEvent(DISPID_PlayEvent, &dispparamsNoArgs);
1026 };
1027
1028 void VLCPlugin::fireOnPauseEvent(void)
1029 {
1030     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1031     vlcConnectionPointContainer->fireEvent(DISPID_PauseEvent, &dispparamsNoArgs);
1032 };
1033
1034 void VLCPlugin::fireOnStopEvent(void)
1035 {
1036     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1037     vlcConnectionPointContainer->fireEvent(DISPID_StopEvent, &dispparamsNoArgs);
1038 };