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"
37 #include "dataobject.h"
46 ////////////////////////////////////////////////////////////////////////
49 // {E23FE9C6-778E-49d4-B537-38FCDE4887D8}
50 //const GUID CLSID_VLCPlugin =
51 // { 0xe23fe9c6, 0x778e, 0x49d4, { 0xb5, 0x37, 0x38, 0xfc, 0xde, 0x48, 0x87, 0xd8 } };
53 static LRESULT CALLBACK VLCInPlaceClassWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
61 if( GetUpdateRect(hWnd, NULL, FALSE) )
63 BeginPaint(hWnd, &ps);
69 return DefWindowProc(hWnd, uMsg, wParam, lParam);
73 static LRESULT CALLBACK VLCVideoClassWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
74 VLCPlugin *p_instance = reinterpret_cast<VLCPlugin *>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
84 if( GetUpdateRect(hWnd, &pr, FALSE) )
87 GetClientRect(hWnd, &bounds);
88 BeginPaint(hWnd, &ps);
89 p_instance->onPaint(ps.hdc, bounds, pr);
95 return DefWindowProc(hWnd, uMsg, wParam, lParam);
99 VLCPluginClass::VLCPluginClass(LONG *p_class_ref, HINSTANCE hInstance) :
100 _p_class_ref(p_class_ref),
101 _hinstance(hInstance)
105 if( ! GetClassInfo(hInstance, getInPlaceWndClassName(), &wClass) )
107 wClass.style = CS_NOCLOSE|CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
108 wClass.lpfnWndProc = VLCInPlaceClassWndProc;
109 wClass.cbClsExtra = 0;
110 wClass.cbWndExtra = 0;
111 wClass.hInstance = hInstance;
113 wClass.hCursor = LoadCursor(NULL, IDC_ARROW);
114 wClass.hbrBackground = NULL;
115 wClass.lpszMenuName = NULL;
116 wClass.lpszClassName = getInPlaceWndClassName();
118 _inplace_wndclass_atom = RegisterClass(&wClass);
122 _inplace_wndclass_atom = 0;
125 if( ! GetClassInfo(hInstance, getVideoWndClassName(), &wClass) )
127 wClass.style = CS_NOCLOSE|CS_HREDRAW|CS_VREDRAW;
128 wClass.lpfnWndProc = VLCVideoClassWndProc;
129 wClass.cbClsExtra = 0;
130 wClass.cbWndExtra = 0;
131 wClass.hInstance = hInstance;
133 wClass.hCursor = LoadCursor(NULL, IDC_ARROW);
134 wClass.hbrBackground = NULL;
135 wClass.lpszMenuName = NULL;
136 wClass.lpszClassName = getVideoWndClassName();
138 _video_wndclass_atom = RegisterClass(&wClass);
142 _video_wndclass_atom = 0;
145 _inplace_hbitmap = (HBITMAP)LoadImage(getHInstance(), TEXT("INPLACE-PICT"), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
150 VLCPluginClass::~VLCPluginClass()
152 if( 0 != _inplace_wndclass_atom )
153 UnregisterClass(MAKEINTATOM(_inplace_wndclass_atom), _hinstance);
155 if( 0 != _video_wndclass_atom )
156 UnregisterClass(MAKEINTATOM(_video_wndclass_atom), _hinstance);
158 if( NULL != _inplace_hbitmap )
159 DeleteObject(_inplace_hbitmap);
162 STDMETHODIMP VLCPluginClass::QueryInterface(REFIID riid, void **ppv)
167 if( (IID_IUnknown == riid) || (IID_IClassFactory == riid) )
170 *ppv = reinterpret_cast<LPVOID>(this);
177 return E_NOINTERFACE;
180 STDMETHODIMP_(ULONG) VLCPluginClass::AddRef(void)
182 return InterlockedIncrement(_p_class_ref);
185 STDMETHODIMP_(ULONG) VLCPluginClass::Release(void)
187 ULONG refcount = InterlockedDecrement(_p_class_ref);
196 STDMETHODIMP VLCPluginClass::CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppv)
203 if( NULL != pUnkOuter )
204 return CLASS_E_NOAGGREGATION;
206 VLCPlugin *plugin = new VLCPlugin(this);
209 HRESULT hr = plugin->QueryInterface(riid, ppv);
213 return E_OUTOFMEMORY;
216 STDMETHODIMP VLCPluginClass::LockServer(BOOL fLock)
226 ////////////////////////////////////////////////////////////////////////
228 VLCPlugin::VLCPlugin(VLCPluginClass *p_class) :
243 vlcOleObject = new VLCOleObject(this);
244 vlcOleControl = new VLCOleControl(this);
245 vlcOleInPlaceObject = new VLCOleInPlaceObject(this);
246 vlcOleInPlaceActiveObject = new VLCOleInPlaceActiveObject(this);
247 vlcPersistStorage = new VLCPersistStorage(this);
248 vlcPersistStreamInit = new VLCPersistStreamInit(this);
249 vlcPersistPropertyBag = new VLCPersistPropertyBag(this);
250 vlcProvideClassInfo = new VLCProvideClassInfo(this);
251 vlcConnectionPointContainer = new VLCConnectionPointContainer(this);
252 vlcObjectSafety = new VLCObjectSafety(this);
253 vlcControl = new VLCControl(this);
254 vlcViewObject = new VLCViewObject(this);
256 // set default/preferred size (320x240) pixels in HIMETRIC
257 HDC hDC = CreateDevDC(NULL);
258 _extent.cx = (320*2540L)/GetDeviceCaps(hDC, LOGPIXELSX);
259 _extent.cy = (240*2540L)/GetDeviceCaps(hDC, LOGPIXELSY);
263 VLCPlugin::~VLCPlugin()
265 vlcOleInPlaceObject->UIDeactivate();
266 vlcOleInPlaceObject->InPlaceDeactivate();
268 delete vlcViewObject;
270 delete vlcObjectSafety;
271 delete vlcConnectionPointContainer;
272 delete vlcProvideClassInfo;
273 delete vlcPersistPropertyBag;
274 delete vlcPersistStreamInit;
275 delete vlcPersistStorage;
276 delete vlcOleInPlaceActiveObject;
277 delete vlcOleInPlaceObject;
278 delete vlcOleControl;
287 STDMETHODIMP VLCPlugin::QueryInterface(REFIID riid, void **ppv)
292 if( IID_IUnknown == riid )
295 *ppv = reinterpret_cast<LPVOID>(this);
298 else if( IID_IOleObject == riid )
301 *ppv = reinterpret_cast<LPVOID>(vlcOleObject);
304 else if( IID_IOleControl == riid )
307 *ppv = reinterpret_cast<LPVOID>(vlcOleControl);
310 else if( IID_IOleWindow == riid )
313 *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
316 else if( IID_IOleInPlaceObject == riid )
319 *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
322 else if( IID_IOleInPlaceActiveObject == riid )
325 *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceActiveObject);
328 else if( IID_IPersist == riid )
331 *ppv = reinterpret_cast<LPVOID>(vlcPersistPropertyBag);
334 else if( IID_IPersistStreamInit == riid )
337 *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
340 else if( IID_IPersistStorage == riid )
343 *ppv = reinterpret_cast<LPVOID>(vlcPersistStorage);
346 else if( IID_IPersistPropertyBag == riid )
349 *ppv = reinterpret_cast<LPVOID>(vlcPersistPropertyBag);
352 else if( IID_IProvideClassInfo == riid )
355 *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
358 else if( IID_IProvideClassInfo2 == riid )
361 *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
364 else if( IID_IConnectionPointContainer == riid )
367 *ppv = reinterpret_cast<LPVOID>(vlcConnectionPointContainer);
370 else if( IID_IObjectSafety == riid )
373 *ppv = reinterpret_cast<LPVOID>(vlcObjectSafety);
376 else if( IID_IDispatch == riid )
379 *ppv = reinterpret_cast<LPVOID>(vlcControl);
382 else if( IID_IVLCControl == riid )
385 *ppv = reinterpret_cast<LPVOID>(vlcControl);
388 else if( IID_IViewObject == riid )
391 *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
394 else if( IID_IViewObject2 == riid )
397 *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
403 return E_NOINTERFACE;
406 STDMETHODIMP_(ULONG) VLCPlugin::AddRef(void)
408 return InterlockedIncrement((LONG *)&_i_ref);
411 STDMETHODIMP_(ULONG) VLCPlugin::Release(void)
413 if( ! InterlockedDecrement((LONG *)&_i_ref) )
421 //////////////////////////////////////
424 ** we use a window to represent plugin viewport,
425 ** whose geometry is limited by the clipping rectangle
426 ** all drawing within this window must follow must
427 ** follow coordinates system described in lprPosRect
430 static void getViewportCoords(LPRECT lprPosRect, LPRECT lprClipRect)
433 bounds.right = lprPosRect->right-lprPosRect->left;
435 if( lprClipRect->left <= lprPosRect->left )
437 // left side is not clipped out
440 if( lprClipRect->right >= lprPosRect->right )
442 // right side is not clipped out, no change
444 else if( lprClipRect->right >= lprPosRect->left )
446 // right side is clipped out
447 lprPosRect->right = lprClipRect->right;
451 // outside of clipping rectange, not visible
452 lprPosRect->right = lprPosRect->left;
457 // left side is clipped out
458 bounds.left = lprPosRect->left-lprClipRect->left;
459 bounds.right += bounds.left;
461 lprPosRect->left = lprClipRect->left;
462 if( lprClipRect->right >= lprPosRect->right )
464 // right side is not clipped out
468 // right side is clipped out
469 lprPosRect->right = lprClipRect->right;
473 bounds.bottom = lprPosRect->bottom-lprPosRect->top;
475 if( lprClipRect->top <= lprPosRect->top )
477 // top side is not clipped out
480 if( lprClipRect->bottom >= lprPosRect->bottom )
482 // bottom side is not clipped out, no change
484 else if( lprClipRect->bottom >= lprPosRect->top )
486 // bottom side is clipped out
487 lprPosRect->bottom = lprClipRect->bottom;
491 // outside of clipping rectange, not visible
492 lprPosRect->right = lprPosRect->left;
497 bounds.top = lprPosRect->top-lprClipRect->top;
498 bounds.bottom += bounds.top;
500 lprPosRect->top = lprClipRect->top;
501 if( lprClipRect->bottom >= lprPosRect->bottom )
503 // bottom side is not clipped out
507 // bottom side is clipped out
508 lprPosRect->bottom = lprClipRect->bottom;
511 *lprClipRect = *lprPosRect;
512 *lprPosRect = bounds;
515 HRESULT VLCPlugin::onInit(void)
520 char *ppsz_argv[] = { "vlc", "-vvv", "--fast-mutex", "--win9x-cv-method=1" };
522 char *ppsz_argv[] = { "vlc", "-vv" };
525 DWORD i_type, i_data = MAX_PATH + 1;
526 char p_data[MAX_PATH + 1];
527 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
528 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
530 if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
531 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
533 if( i_type == REG_SZ )
535 strcat( p_data, "\\vlc" );
536 ppsz_argv[0] = p_data;
539 RegCloseKey( h_key );
543 ppsz_argv[0] = "C:\\cygwin\\home\\Damien_Fouilleul\\dev\\videolan\\vlc-trunk\\vlc";
546 _i_vlc = VLC_Create();
548 if( VLC_Init(_i_vlc, sizeof(ppsz_argv)/sizeof(char*), ppsz_argv) )
559 HRESULT VLCPlugin::onLoad(void)
562 VLC_VolumeMute(_i_vlc);
564 if( NULL != _psz_src )
566 // add default target to playlist
568 int cOptionsCount = 0;
572 cOptions[cOptionsCount++] = "loop";
574 VLC_AddTarget(_i_vlc, _psz_src, (const char **)&cOptions, cOptionsCount, PLAYLIST_APPEND, PLAYLIST_END);
579 HRESULT VLCPlugin::onClientSiteChanged(LPOLECLIENTSITE pActiveSite)
581 if( NULL != pActiveSite )
584 ** object is embedded in container
585 ** try to activate in place if it has initialized
589 vlcOleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, pActiveSite, 0, NULL, NULL);
595 HRESULT VLCPlugin::onClose(DWORD dwSaveOption)
599 if( isInPlaceActive() )
601 onInPlaceDeactivate();
611 BOOL VLCPlugin::isInPlaceActive(void)
613 return (NULL != _inplacewnd);
616 HRESULT VLCPlugin::onActivateInPlace(LPMSG lpMesg, HWND hwndParent, LPCRECT lprcPosRect, LPCRECT lprcClipRect)
618 RECT posRect = *lprcPosRect;
619 RECT clipRect = *lprcClipRect;
622 ** record keeping of control geometry within container
627 ** convert posRect & clipRect to match control viewport coordinates
629 getViewportCoords(&posRect, &clipRect);
632 ** Create a window for in place activated control.
633 ** the window geometry represents the control viewport
634 ** so that embedded video is always properly clipped.
636 _inplacewnd = CreateWindow(_p_class->getInPlaceWndClassName(),
637 "VLC Plugin In-Place Window",
638 WS_CHILD|WS_CLIPCHILDREN|WS_TABSTOP,
641 clipRect.right-clipRect.left,
642 clipRect.bottom-clipRect.top,
645 _p_class->getHInstance(),
649 if( NULL == _inplacewnd )
652 SetWindowLongPtr(_inplacewnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
655 ** VLC embedded video geometry automatically matches parent window.
656 ** hence create a child window so that video position and size
657 ** is always correct relative to the viewport bounds
659 _videownd = CreateWindow(_p_class->getVideoWndClassName(),
660 "VLC Plugin Video Window",
661 WS_CHILD|WS_CLIPCHILDREN|WS_VISIBLE,
664 posRect.right-posRect.left,
665 posRect.bottom-posRect.top,
668 _p_class->getHInstance(),
672 if( NULL == _videownd )
674 DestroyWindow(_inplacewnd);
678 SetWindowLongPtr(_videownd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
681 ShowWindow(_inplacewnd, SW_SHOWNORMAL);
683 /* horrible cast there */
685 val.i_int = reinterpret_cast<int>(_videownd);
686 VLC_VariableSet(_i_vlc, "drawable", val);
688 if( _b_autostart & (VLC_PlaylistNumberOfItems(_i_vlc) > 0) )
696 HRESULT VLCPlugin::onInPlaceDeactivate(void)
701 DestroyWindow(_videownd);
703 DestroyWindow(_inplacewnd);
709 void VLCPlugin::setVisible(BOOL fVisible)
711 _b_visible = fVisible;
712 if( isInPlaceActive() )
713 ShowWindow(_inplacewnd, fVisible ? SW_SHOWNORMAL : SW_HIDE);
714 firePropChangedEvent(DISPID_Visible);
717 void VLCPlugin::setFocus(BOOL fFocus)
720 SetActiveWindow(_inplacewnd);
723 BOOL VLCPlugin::hasFocus(void)
725 return GetActiveWindow() == _inplacewnd;
728 void VLCPlugin::onPaint(HDC hdc, const RECT &bounds, const RECT &pr)
733 ** if VLC is playing, it may not display any VIDEO content
734 ** hence, draw control logo
736 int width = bounds.right-bounds.left;
737 int height = bounds.bottom-bounds.top;
739 HBITMAP pict = _p_class->getInPlacePict();
742 HDC hdcPict = CreateCompatibleDC(hdc);
743 if( NULL != hdcPict )
746 if( GetObject(pict, sizeof(BITMAPINFO), &bm) )
748 int dstWidth = bm.bmWidth;
749 if( dstWidth > width-4 )
752 int dstHeight = bm.bmHeight;
753 if( dstHeight > height-4 )
754 dstHeight = height-4;
756 int dstX = bounds.left+(width-dstWidth)/2;
757 int dstY = bounds.top+(height-dstHeight)/2;
759 SelectObject(hdcPict, pict);
760 StretchBlt(hdc, dstX, dstY, dstWidth, dstHeight,
761 hdcPict, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
763 ExcludeClipRect(hdc, dstX, dstY, dstWidth+dstX, dstHeight+dstY);
768 FillRect(hdc, &pr, (HBRUSH)GetStockObject(WHITE_BRUSH));
769 SelectObject(hdc, GetStockObject(BLACK_BRUSH));
771 MoveToEx(hdc, bounds.left, bounds.top, NULL);
772 LineTo(hdc, bounds.left+width-1, bounds.top);
773 LineTo(hdc, bounds.left+width-1, bounds.top+height-1);
774 LineTo(hdc, bounds.left, bounds.top+height-1);
775 LineTo(hdc, bounds.left, bounds.top);
779 void VLCPlugin::onPositionChange(LPCRECT lprcPosRect, LPCRECT lprcClipRect)
781 RECT clipRect = *lprcClipRect;
782 RECT posRect = *lprcPosRect;
785 ** tell container that previous area needs redrawing
788 InvalidateRect(GetParent(_inplacewnd), &_posRect, TRUE);
791 ** record keeping of control geometry within container
797 ** convert posRect & clipRect to match control viewport coordinates
799 getViewportCoords(&posRect, &clipRect);
802 ** change in-place window geometry to match clipping region
804 MoveWindow(_inplacewnd,
807 clipRect.right-clipRect.left,
808 clipRect.bottom-clipRect.top,
812 ** change video window geometry to match object bounds within clipping region
814 MoveWindow(_videownd,
817 posRect.right-posRect.left,
818 posRect.bottom-posRect.top,
822 ** force a full refresh of control content
825 updateRect.left = -posRect.left;
826 updateRect.top = -posRect.top;
827 updateRect.right = posRect.right-posRect.left;
828 updateRect.bottom = posRect.bottom-posRect.top;
830 ValidateRect(_videownd, NULL);
831 InvalidateRect(_videownd, &updateRect, FALSE);
832 UpdateWindow(_videownd);
835 void VLCPlugin::firePropChangedEvent(DISPID dispid)
839 vlcConnectionPointContainer->firePropChangedEvent(dispid);
843 void VLCPlugin::fireOnPlayEvent(void)
847 DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
848 vlcConnectionPointContainer->fireEvent(DISPID_PlayEvent, &dispparamsNoArgs);
852 void VLCPlugin::fireOnPauseEvent(void)
856 DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
857 vlcConnectionPointContainer->fireEvent(DISPID_PauseEvent, &dispparamsNoArgs);
861 void VLCPlugin::fireOnStopEvent(void)
865 DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
866 vlcConnectionPointContainer->fireEvent(DISPID_StopEvent, &dispparamsNoArgs);