1 /*****************************************************************************
2 * plugin.cpp: ActiveX control for VLC
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN
6 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
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 "viewobject.h"
45 ////////////////////////////////////////////////////////////////////////
48 // {E23FE9C6-778E-49d4-B537-38FCDE4887D8}
49 //const GUID CLSID_VLCPlugin =
50 // { 0xe23fe9c6, 0x778e, 0x49d4, { 0xb5, 0x37, 0x38, 0xfc, 0xde, 0x48, 0x87, 0xd8 } };
52 static LRESULT CALLBACK VLCInPlaceClassWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
60 if( GetUpdateRect(hWnd, NULL, FALSE) )
62 BeginPaint(hWnd, &ps);
68 return DefWindowProc(hWnd, uMsg, wParam, lParam);
72 static LRESULT CALLBACK VLCVideoClassWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
73 VLCPlugin *p_instance = reinterpret_cast<VLCPlugin *>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
83 if( GetUpdateRect(hWnd, &pr, FALSE) )
86 GetClientRect(hWnd, &bounds);
87 BeginPaint(hWnd, &ps);
88 p_instance->onPaint(ps.hdc, bounds, pr);
94 return DefWindowProc(hWnd, uMsg, wParam, lParam);
98 VLCPluginClass::VLCPluginClass(LONG *p_class_ref, HINSTANCE hInstance) :
99 _p_class_ref(p_class_ref),
100 _hinstance(hInstance)
104 if( ! GetClassInfo(hInstance, getInPlaceWndClassName(), &wClass) )
106 wClass.style = CS_NOCLOSE|CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
107 wClass.lpfnWndProc = VLCInPlaceClassWndProc;
108 wClass.cbClsExtra = 0;
109 wClass.cbWndExtra = 0;
110 wClass.hInstance = hInstance;
112 wClass.hCursor = LoadCursor(NULL, IDC_ARROW);
113 wClass.hbrBackground = NULL;
114 wClass.lpszMenuName = NULL;
115 wClass.lpszClassName = getInPlaceWndClassName();
117 _inplace_wndclass_atom = RegisterClass(&wClass);
121 _inplace_wndclass_atom = 0;
124 if( ! GetClassInfo(hInstance, getVideoWndClassName(), &wClass) )
126 wClass.style = CS_NOCLOSE|CS_HREDRAW|CS_VREDRAW;
127 wClass.lpfnWndProc = VLCVideoClassWndProc;
128 wClass.cbClsExtra = 0;
129 wClass.cbWndExtra = 0;
130 wClass.hInstance = hInstance;
132 wClass.hCursor = LoadCursor(NULL, IDC_ARROW);
133 wClass.hbrBackground = NULL;
134 wClass.lpszMenuName = NULL;
135 wClass.lpszClassName = getVideoWndClassName();
137 _video_wndclass_atom = RegisterClass(&wClass);
141 _video_wndclass_atom = 0;
144 _inplace_hbitmap = (HBITMAP)LoadImage(getHInstance(), TEXT("INPLACE-PICT"), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
149 VLCPluginClass::~VLCPluginClass()
151 if( 0 != _inplace_wndclass_atom )
152 UnregisterClass(MAKEINTATOM(_inplace_wndclass_atom), _hinstance);
154 if( 0 != _video_wndclass_atom )
155 UnregisterClass(MAKEINTATOM(_video_wndclass_atom), _hinstance);
157 if( NULL != _inplace_hbitmap )
158 DeleteObject(_inplace_hbitmap);
161 STDMETHODIMP VLCPluginClass::QueryInterface(REFIID riid, void **ppv)
166 if( (IID_IUnknown == riid) || (IID_IClassFactory == riid) )
169 *ppv = reinterpret_cast<LPVOID>(this);
176 return E_NOINTERFACE;
179 STDMETHODIMP_(ULONG) VLCPluginClass::AddRef(void)
181 return InterlockedIncrement(_p_class_ref);
184 STDMETHODIMP_(ULONG) VLCPluginClass::Release(void)
186 ULONG refcount = InterlockedDecrement(_p_class_ref);
195 STDMETHODIMP VLCPluginClass::CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppv)
202 if( NULL != pUnkOuter )
203 return CLASS_E_NOAGGREGATION;
205 VLCPlugin *plugin = new VLCPlugin(this);
208 HRESULT hr = plugin->QueryInterface(riid, ppv);
212 return E_OUTOFMEMORY;
215 STDMETHODIMP VLCPluginClass::LockServer(BOOL fLock)
225 ////////////////////////////////////////////////////////////////////////
227 VLCPlugin::VLCPlugin(VLCPluginClass *p_class) :
241 vlcOleObject = new VLCOleObject(this);
242 vlcOleControl = new VLCOleControl(this);
243 vlcOleInPlaceObject = new VLCOleInPlaceObject(this);
244 vlcOleInPlaceActiveObject = new VLCOleInPlaceActiveObject(this);
245 vlcPersistStorage = new VLCPersistStorage(this);
246 vlcPersistStreamInit = new VLCPersistStreamInit(this);
247 vlcPersistPropertyBag = new VLCPersistPropertyBag(this);
248 vlcProvideClassInfo = new VLCProvideClassInfo(this);
249 vlcConnectionPointContainer = new VLCConnectionPointContainer(this);
250 vlcObjectSafety = new VLCObjectSafety(this);
251 vlcControl = new VLCControl(this);
252 vlcViewObject = new VLCViewObject(this);
254 // set default/preferred size (320x240) pixels in HIMETRIC
255 HDC hDC = CreateDevDC(NULL);
256 _extent.cx = (320*2540L)/GetDeviceCaps(hDC, LOGPIXELSX);
257 _extent.cy = (240*2540L)/GetDeviceCaps(hDC, LOGPIXELSY);
261 VLCPlugin::~VLCPlugin()
263 vlcOleInPlaceObject->UIDeactivate();
264 vlcOleInPlaceObject->InPlaceDeactivate();
266 delete vlcViewObject;
268 delete vlcObjectSafety;
269 delete vlcConnectionPointContainer;
270 delete vlcProvideClassInfo;
271 delete vlcPersistPropertyBag;
272 delete vlcPersistStreamInit;
273 delete vlcPersistStorage;
274 delete vlcOleInPlaceActiveObject;
275 delete vlcOleInPlaceObject;
276 delete vlcOleControl;
285 STDMETHODIMP VLCPlugin::QueryInterface(REFIID riid, void **ppv)
290 if( IID_IUnknown == riid )
293 *ppv = reinterpret_cast<LPVOID>(this);
296 else if( IID_IOleObject == riid )
299 *ppv = reinterpret_cast<LPVOID>(vlcOleObject);
302 else if( IID_IOleControl == riid )
305 *ppv = reinterpret_cast<LPVOID>(vlcOleControl);
308 else if( IID_IOleWindow == riid )
311 *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
314 else if( IID_IOleInPlaceObject == riid )
317 *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
320 else if( IID_IOleInPlaceActiveObject == riid )
323 *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceActiveObject);
326 else if( IID_IPersist == riid )
329 *ppv = reinterpret_cast<LPVOID>(vlcPersistPropertyBag);
332 else if( IID_IPersistStreamInit == riid )
335 *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
338 else if( IID_IPersistStorage == riid )
341 *ppv = reinterpret_cast<LPVOID>(vlcPersistStorage);
344 else if( IID_IPersistPropertyBag == riid )
347 *ppv = reinterpret_cast<LPVOID>(vlcPersistPropertyBag);
350 else if( IID_IProvideClassInfo == riid )
353 *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
356 else if( IID_IProvideClassInfo2 == riid )
359 *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
362 else if( IID_IConnectionPointContainer == riid )
365 *ppv = reinterpret_cast<LPVOID>(vlcConnectionPointContainer);
368 else if( IID_IObjectSafety == riid )
371 *ppv = reinterpret_cast<LPVOID>(vlcObjectSafety);
374 else if( IID_IDispatch == riid )
377 *ppv = reinterpret_cast<LPVOID>(vlcControl);
380 else if( IID_IVLCControl == riid )
383 *ppv = reinterpret_cast<LPVOID>(vlcControl);
386 else if( IID_IViewObject == riid )
389 *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
392 else if( IID_IViewObject2 == riid )
395 *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
401 return E_NOINTERFACE;
404 STDMETHODIMP_(ULONG) VLCPlugin::AddRef(void)
406 return InterlockedIncrement((LONG *)&_i_ref);
409 STDMETHODIMP_(ULONG) VLCPlugin::Release(void)
411 if( ! InterlockedDecrement((LONG *)&_i_ref) )
419 //////////////////////////////////////
422 ** we use a window to represent plugin viewport,
423 ** whose geometry is limited by the clipping rectangle
424 ** all drawing within this window must follow must
425 ** follow coordinates system described in lprPosRect
428 static void getViewportCoords(LPRECT lprPosRect, LPRECT lprClipRect)
431 bounds.right = lprPosRect->right-lprPosRect->left;
433 if( lprClipRect->left <= lprPosRect->left )
435 // left side is not clipped out
438 if( lprClipRect->right >= lprPosRect->right )
440 // right side is not clipped out, no change
442 else if( lprClipRect->right >= lprPosRect->left )
444 // right side is clipped out
445 lprPosRect->right = lprClipRect->right;
449 // outside of clipping rectange, not visible
450 lprPosRect->right = lprPosRect->left;
455 // left side is clipped out
456 bounds.left = lprPosRect->left-lprClipRect->left;
457 bounds.right += bounds.left;
459 lprPosRect->left = lprClipRect->left;
460 if( lprClipRect->right >= lprPosRect->right )
462 // right side is not clipped out
466 // right side is clipped out
467 lprPosRect->right = lprClipRect->right;
471 bounds.bottom = lprPosRect->bottom-lprPosRect->top;
473 if( lprClipRect->top <= lprPosRect->top )
475 // top side is not clipped out
478 if( lprClipRect->bottom >= lprPosRect->bottom )
480 // bottom side is not clipped out, no change
482 else if( lprClipRect->bottom >= lprPosRect->top )
484 // bottom side is clipped out
485 lprPosRect->bottom = lprClipRect->bottom;
489 // outside of clipping rectange, not visible
490 lprPosRect->right = lprPosRect->left;
495 bounds.top = lprPosRect->top-lprClipRect->top;
496 bounds.bottom += bounds.top;
498 lprPosRect->top = lprClipRect->top;
499 if( lprClipRect->bottom >= lprPosRect->bottom )
501 // bottom side is not clipped out
505 // bottom side is clipped out
506 lprPosRect->bottom = lprClipRect->bottom;
509 *lprClipRect = *lprPosRect;
510 *lprPosRect = bounds;
513 HRESULT VLCPlugin::onInit(BOOL isNew)
518 char *ppsz_argv[] = { "vlc", "-vvv", "--fast-mutex", "--win9x-cv-method=1" };
520 char *ppsz_argv[] = { "vlc", "-vv" };
523 DWORD i_type, i_data = MAX_PATH + 1;
524 char p_data[MAX_PATH + 1];
525 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
526 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
528 if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
529 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
531 if( i_type == REG_SZ )
533 strcat( p_data, "\\vlc" );
534 ppsz_argv[0] = p_data;
537 RegCloseKey( h_key );
541 ppsz_argv[0] = "C:\\cygwin\\home\\Damien_Fouilleul\\dev\\videolan\\vlc-trunk\\vlc";
544 _i_vlc = VLC_Create();
546 if( VLC_Init(_i_vlc, sizeof(ppsz_argv)/sizeof(char*), ppsz_argv) )
556 ** object has fully initialized,
557 ** try to activate in place if container is ready
559 LPOLECLIENTSITE pActiveSite;
561 if( SUCCEEDED(vlcOleObject->GetClientSite(&pActiveSite)) && (NULL != pActiveSite) )
563 vlcOleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, pActiveSite, 0, NULL, NULL);
564 pActiveSite->Release();
572 HRESULT VLCPlugin::onLoad(void)
575 ** object has fully initialized,
576 ** try to activate in place if container is ready
578 LPOLECLIENTSITE pActiveSite;
580 if( SUCCEEDED(vlcOleObject->GetClientSite(&pActiveSite)) && (NULL != pActiveSite) )
582 vlcOleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, pActiveSite, 0, NULL, NULL);
583 pActiveSite->Release();
588 HRESULT VLCPlugin::onClientSiteChanged(LPOLECLIENTSITE pActiveSite)
590 if( NULL != pActiveSite )
593 ** object is embedded in container
594 ** try to activate in place if it has initialized
598 vlcOleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, pActiveSite, 0, NULL, NULL);
604 HRESULT VLCPlugin::onClose(DWORD dwSaveOption)
608 if( isInPlaceActive() )
610 onInPlaceDeactivate();
620 BOOL VLCPlugin::isInPlaceActive(void)
622 return (NULL != _inplacewnd);
625 HRESULT VLCPlugin::onActivateInPlace(LPMSG lpMesg, HWND hwndParent, LPCRECT lprcPosRect, LPCRECT lprcClipRect)
627 RECT posRect = *lprcPosRect;
628 RECT clipRect = *lprcClipRect;
631 ** record keeping of control geometry within container
636 ** convert posRect & clipRect to match control viewport coordinates
638 getViewportCoords(&posRect, &clipRect);
641 ** Create a window for in place activated control.
642 ** the window geometry represents the control viewport
643 ** so that embedded video is always properly clipped.
645 _inplacewnd = CreateWindow(_p_class->getInPlaceWndClassName(),
646 "VLC Plugin In-Place Window",
647 WS_CHILD|WS_CLIPCHILDREN|WS_TABSTOP,
650 clipRect.right-clipRect.left,
651 clipRect.bottom-clipRect.top,
654 _p_class->getHInstance(),
658 if( NULL == _inplacewnd )
661 SetWindowLongPtr(_inplacewnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
664 ** VLC embedded video geometry automatically matches parent window.
665 ** hence create a child window so that video position and size
666 ** is always correct relative to the viewport bounds
668 _videownd = CreateWindow(_p_class->getVideoWndClassName(),
669 "VLC Plugin Video Window",
670 WS_CHILD|WS_CLIPCHILDREN|WS_VISIBLE,
673 posRect.right-posRect.left,
674 posRect.bottom-posRect.top,
677 _p_class->getHInstance(),
681 if( NULL == _videownd )
683 DestroyWindow(_inplacewnd);
687 SetWindowLongPtr(_videownd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
690 ShowWindow(_inplacewnd, SW_SHOWNORMAL);
692 /* horrible cast there */
694 val.i_int = reinterpret_cast<int>(_videownd);
695 VLC_VariableSet(_i_vlc, "drawable", val);
697 if( NULL != _psz_src )
699 // add target to playlist
701 int cOptionsCount = 0;
705 cOptions[cOptionsCount++] = "loop";
707 VLC_AddTarget(_i_vlc, _psz_src, (const char **)&cOptions, cOptionsCount, PLAYLIST_APPEND, PLAYLIST_END);
718 HRESULT VLCPlugin::onInPlaceDeactivate(void)
723 DestroyWindow(_videownd);
725 DestroyWindow(_inplacewnd);
731 void VLCPlugin::setVisible(BOOL fVisible)
733 _b_visible = fVisible;
734 if( isInPlaceActive() )
735 ShowWindow(_inplacewnd, fVisible ? SW_SHOWNORMAL : SW_HIDE);
736 firePropChangedEvent(DISPID_Visible);
739 void VLCPlugin::setFocus(BOOL fFocus)
742 SetActiveWindow(_inplacewnd);
745 BOOL VLCPlugin::hasFocus(void)
747 return GetActiveWindow() == _inplacewnd;
750 void VLCPlugin::onPaint(HDC hdc, const RECT &bounds, const RECT &pr)
753 ** if VLC is playing, it may not display any VIDEO content
754 ** hence, draw control logo
756 int width = bounds.right-bounds.left;
757 int height = bounds.bottom-bounds.top;
759 HBITMAP pict = _p_class->getInPlacePict();
762 HDC hdcPict = CreateCompatibleDC(hdc);
763 if( NULL != hdcPict )
766 if( GetObject(pict, sizeof(BITMAPINFO), &bm) )
768 int dstWidth = bm.bmWidth;
769 if( dstWidth > width-4 )
772 int dstHeight = bm.bmHeight;
773 if( dstHeight > height-4 )
774 dstHeight = height-4;
776 int dstX = bounds.left+(width-dstWidth)/2;
777 int dstY = bounds.top+(height-dstHeight)/2;
779 SelectObject(hdcPict, pict);
780 StretchBlt(hdc, dstX, dstY, dstWidth, dstHeight,
781 hdcPict, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
783 ExcludeClipRect(hdc, dstX, dstY, dstWidth+dstX, dstHeight+dstY);
788 FillRect(hdc, &pr, (HBRUSH)GetStockObject(WHITE_BRUSH));
789 SelectObject(hdc, GetStockObject(BLACK_BRUSH));
791 MoveToEx(hdc, bounds.left, bounds.top, NULL);
792 LineTo(hdc, bounds.left+width-1, bounds.top);
793 LineTo(hdc, bounds.left+width-1, bounds.top+height-1);
794 LineTo(hdc, bounds.left, bounds.top+height-1);
795 LineTo(hdc, bounds.left, bounds.top);
798 void VLCPlugin::onPositionChange(LPCRECT lprcPosRect, LPCRECT lprcClipRect)
800 RECT clipRect = *lprcClipRect;
801 RECT posRect = *lprcPosRect;
804 ** record keeping of control geometry within container
809 ** convert posRect & clipRect to match control viewport coordinates
811 getViewportCoords(&posRect, &clipRect);
814 ** change in-place window geometry to match clipping region
816 MoveWindow(_inplacewnd,
819 clipRect.right-clipRect.left,
820 clipRect.bottom-clipRect.top,
824 ** change video window geometry to match object bounds within clipping region
826 MoveWindow(_videownd,
829 posRect.right-posRect.left,
830 posRect.bottom-posRect.top,
835 ** force a full refresh of control content
838 updateRect.left = -posRect.left;
839 updateRect.top = -posRect.top;
840 updateRect.right = posRect.right-posRect.left;
841 updateRect.bottom = posRect.bottom-posRect.top;
843 ValidateRect(_videownd, NULL);
844 InvalidateRect(_videownd, &updateRect, FALSE);
845 UpdateWindow(_videownd);
848 void VLCPlugin::firePropChangedEvent(DISPID dispid)
852 vlcConnectionPointContainer->firePropChangedEvent(dispid);
856 void VLCPlugin::fireOnPlayEvent(void)
860 DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
861 vlcConnectionPointContainer->fireEvent(DISPID_PlayEvent, &dispparamsNoArgs);
865 void VLCPlugin::fireOnPauseEvent(void)
869 DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
870 vlcConnectionPointContainer->fireEvent(DISPID_PauseEvent, &dispparamsNoArgs);
874 void VLCPlugin::fireOnStopEvent(void)
878 DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
879 vlcConnectionPointContainer->fireEvent(DISPID_StopEvent, &dispparamsNoArgs);