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