1 /*****************************************************************************
2 * plugin.cpp: ActiveX control for VLC
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN (Centrale Réseaux) and its contributors
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) :
242 vlcOleObject = new VLCOleObject(this);
243 vlcOleControl = new VLCOleControl(this);
244 vlcOleInPlaceObject = new VLCOleInPlaceObject(this);
245 vlcOleInPlaceActiveObject = new VLCOleInPlaceActiveObject(this);
246 vlcPersistStorage = new VLCPersistStorage(this);
247 vlcPersistStreamInit = new VLCPersistStreamInit(this);
248 vlcPersistPropertyBag = new VLCPersistPropertyBag(this);
249 vlcProvideClassInfo = new VLCProvideClassInfo(this);
250 vlcConnectionPointContainer = new VLCConnectionPointContainer(this);
251 vlcObjectSafety = new VLCObjectSafety(this);
252 vlcControl = new VLCControl(this);
253 vlcViewObject = new VLCViewObject(this);
255 // set default/preferred size (320x240) pixels in HIMETRIC
256 HDC hDC = CreateDevDC(NULL);
257 _extent.cx = (320*2540L)/GetDeviceCaps(hDC, LOGPIXELSX);
258 _extent.cy = (240*2540L)/GetDeviceCaps(hDC, LOGPIXELSY);
262 VLCPlugin::~VLCPlugin()
264 vlcOleInPlaceObject->UIDeactivate();
265 vlcOleInPlaceObject->InPlaceDeactivate();
267 delete vlcViewObject;
269 delete vlcObjectSafety;
270 delete vlcConnectionPointContainer;
271 delete vlcProvideClassInfo;
272 delete vlcPersistPropertyBag;
273 delete vlcPersistStreamInit;
274 delete vlcPersistStorage;
275 delete vlcOleInPlaceActiveObject;
276 delete vlcOleInPlaceObject;
277 delete vlcOleControl;
286 STDMETHODIMP VLCPlugin::QueryInterface(REFIID riid, void **ppv)
291 if( IID_IUnknown == riid )
294 *ppv = reinterpret_cast<LPVOID>(this);
297 else if( IID_IOleObject == riid )
300 *ppv = reinterpret_cast<LPVOID>(vlcOleObject);
303 else if( IID_IOleControl == riid )
306 *ppv = reinterpret_cast<LPVOID>(vlcOleControl);
309 else if( IID_IOleWindow == riid )
312 *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
315 else if( IID_IOleInPlaceObject == riid )
318 *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
321 else if( IID_IOleInPlaceActiveObject == riid )
324 *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceActiveObject);
327 else if( IID_IPersist == riid )
330 *ppv = reinterpret_cast<LPVOID>(vlcPersistPropertyBag);
333 else if( IID_IPersistStreamInit == riid )
336 *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
339 else if( IID_IPersistStorage == riid )
342 *ppv = reinterpret_cast<LPVOID>(vlcPersistStorage);
345 else if( IID_IPersistPropertyBag == riid )
348 *ppv = reinterpret_cast<LPVOID>(vlcPersistPropertyBag);
351 else if( IID_IProvideClassInfo == riid )
354 *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
357 else if( IID_IProvideClassInfo2 == riid )
360 *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
363 else if( IID_IConnectionPointContainer == riid )
366 *ppv = reinterpret_cast<LPVOID>(vlcConnectionPointContainer);
369 else if( IID_IObjectSafety == riid )
372 *ppv = reinterpret_cast<LPVOID>(vlcObjectSafety);
375 else if( IID_IDispatch == riid )
378 *ppv = reinterpret_cast<LPVOID>(vlcControl);
381 else if( IID_IVLCControl == riid )
384 *ppv = reinterpret_cast<LPVOID>(vlcControl);
387 else if( IID_IViewObject == riid )
390 *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
393 else if( IID_IViewObject2 == riid )
396 *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
402 return E_NOINTERFACE;
405 STDMETHODIMP_(ULONG) VLCPlugin::AddRef(void)
407 return InterlockedIncrement((LONG *)&_i_ref);
410 STDMETHODIMP_(ULONG) VLCPlugin::Release(void)
412 if( ! InterlockedDecrement((LONG *)&_i_ref) )
420 //////////////////////////////////////
423 ** we use a window to represent plugin viewport,
424 ** whose geometry is limited by the clipping rectangle
425 ** all drawing within this window must follow must
426 ** follow coordinates system described in lprPosRect
429 static void getViewportCoords(LPRECT lprPosRect, LPRECT lprClipRect)
432 bounds.right = lprPosRect->right-lprPosRect->left;
434 if( lprClipRect->left <= lprPosRect->left )
436 // left side is not clipped out
439 if( lprClipRect->right >= lprPosRect->right )
441 // right side is not clipped out, no change
443 else if( lprClipRect->right >= lprPosRect->left )
445 // right side is clipped out
446 lprPosRect->right = lprClipRect->right;
450 // outside of clipping rectange, not visible
451 lprPosRect->right = lprPosRect->left;
456 // left side is clipped out
457 bounds.left = lprPosRect->left-lprClipRect->left;
458 bounds.right += bounds.left;
460 lprPosRect->left = lprClipRect->left;
461 if( lprClipRect->right >= lprPosRect->right )
463 // right side is not clipped out
467 // right side is clipped out
468 lprPosRect->right = lprClipRect->right;
472 bounds.bottom = lprPosRect->bottom-lprPosRect->top;
474 if( lprClipRect->top <= lprPosRect->top )
476 // top side is not clipped out
479 if( lprClipRect->bottom >= lprPosRect->bottom )
481 // bottom side is not clipped out, no change
483 else if( lprClipRect->bottom >= lprPosRect->top )
485 // bottom side is clipped out
486 lprPosRect->bottom = lprClipRect->bottom;
490 // outside of clipping rectange, not visible
491 lprPosRect->right = lprPosRect->left;
496 bounds.top = lprPosRect->top-lprClipRect->top;
497 bounds.bottom += bounds.top;
499 lprPosRect->top = lprClipRect->top;
500 if( lprClipRect->bottom >= lprPosRect->bottom )
502 // bottom side is not clipped out
506 // bottom side is clipped out
507 lprPosRect->bottom = lprClipRect->bottom;
510 *lprClipRect = *lprPosRect;
511 *lprPosRect = bounds;
514 HRESULT VLCPlugin::onInit(void)
519 char *ppsz_argv[] = { "vlc", "-vvv", "--fast-mutex", "--win9x-cv-method=1" };
521 char *ppsz_argv[] = { "vlc", "-vv" };
524 DWORD i_type, i_data = MAX_PATH + 1;
525 char p_data[MAX_PATH + 1];
526 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
527 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
529 if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
530 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
532 if( i_type == REG_SZ )
534 strcat( p_data, "\\vlc" );
535 ppsz_argv[0] = p_data;
538 RegCloseKey( h_key );
542 ppsz_argv[0] = "C:\\cygwin\\home\\Damien_Fouilleul\\dev\\videolan\\vlc-trunk\\vlc";
545 _i_vlc = VLC_Create();
547 if( VLC_Init(_i_vlc, sizeof(ppsz_argv)/sizeof(char*), ppsz_argv) )
558 HRESULT VLCPlugin::onLoad(void)
561 VLC_VolumeMute(_i_vlc);
563 if( NULL != _psz_src )
565 // add default target to playlist
567 int cOptionsCount = 0;
571 cOptions[cOptionsCount++] = "loop";
573 VLC_AddTarget(_i_vlc, _psz_src, (const char **)&cOptions, cOptionsCount, PLAYLIST_APPEND, PLAYLIST_END);
578 HRESULT VLCPlugin::onClientSiteChanged(LPOLECLIENTSITE pActiveSite)
580 if( NULL != pActiveSite )
583 ** object is embedded in container
584 ** try to activate in place if it has initialized
588 vlcOleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, pActiveSite, 0, NULL, NULL);
594 HRESULT VLCPlugin::onClose(DWORD dwSaveOption)
598 if( isInPlaceActive() )
600 onInPlaceDeactivate();
610 BOOL VLCPlugin::isInPlaceActive(void)
612 return (NULL != _inplacewnd);
615 HRESULT VLCPlugin::onActivateInPlace(LPMSG lpMesg, HWND hwndParent, LPCRECT lprcPosRect, LPCRECT lprcClipRect)
617 RECT posRect = *lprcPosRect;
618 RECT clipRect = *lprcClipRect;
621 ** record keeping of control geometry within container
626 ** convert posRect & clipRect to match control viewport coordinates
628 getViewportCoords(&posRect, &clipRect);
631 ** Create a window for in place activated control.
632 ** the window geometry represents the control viewport
633 ** so that embedded video is always properly clipped.
635 _inplacewnd = CreateWindow(_p_class->getInPlaceWndClassName(),
636 "VLC Plugin In-Place Window",
637 WS_CHILD|WS_CLIPCHILDREN|WS_TABSTOP,
640 clipRect.right-clipRect.left,
641 clipRect.bottom-clipRect.top,
644 _p_class->getHInstance(),
648 if( NULL == _inplacewnd )
651 SetWindowLongPtr(_inplacewnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
654 ** VLC embedded video geometry automatically matches parent window.
655 ** hence create a child window so that video position and size
656 ** is always correct relative to the viewport bounds
658 _videownd = CreateWindow(_p_class->getVideoWndClassName(),
659 "VLC Plugin Video Window",
660 WS_CHILD|WS_CLIPCHILDREN|WS_VISIBLE,
663 posRect.right-posRect.left,
664 posRect.bottom-posRect.top,
667 _p_class->getHInstance(),
671 if( NULL == _videownd )
673 DestroyWindow(_inplacewnd);
677 SetWindowLongPtr(_videownd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
680 ShowWindow(_inplacewnd, SW_SHOWNORMAL);
682 /* horrible cast there */
684 val.i_int = reinterpret_cast<int>(_videownd);
685 VLC_VariableSet(_i_vlc, "drawable", val);
687 if( _b_autostart & (VLC_PlaylistNumberOfItems(_i_vlc) > 0) )
695 HRESULT VLCPlugin::onInPlaceDeactivate(void)
700 DestroyWindow(_videownd);
702 DestroyWindow(_inplacewnd);
708 void VLCPlugin::setVisible(BOOL fVisible)
710 _b_visible = fVisible;
711 if( isInPlaceActive() )
712 ShowWindow(_inplacewnd, fVisible ? SW_SHOWNORMAL : SW_HIDE);
713 firePropChangedEvent(DISPID_Visible);
716 void VLCPlugin::setFocus(BOOL fFocus)
719 SetActiveWindow(_inplacewnd);
722 BOOL VLCPlugin::hasFocus(void)
724 return GetActiveWindow() == _inplacewnd;
727 void VLCPlugin::onPaint(HDC hdc, const RECT &bounds, const RECT &pr)
732 ** if VLC is playing, it may not display any VIDEO content
733 ** hence, draw control logo
735 int width = bounds.right-bounds.left;
736 int height = bounds.bottom-bounds.top;
738 HBITMAP pict = _p_class->getInPlacePict();
741 HDC hdcPict = CreateCompatibleDC(hdc);
742 if( NULL != hdcPict )
745 if( GetObject(pict, sizeof(BITMAPINFO), &bm) )
747 int dstWidth = bm.bmWidth;
748 if( dstWidth > width-4 )
751 int dstHeight = bm.bmHeight;
752 if( dstHeight > height-4 )
753 dstHeight = height-4;
755 int dstX = bounds.left+(width-dstWidth)/2;
756 int dstY = bounds.top+(height-dstHeight)/2;
758 SelectObject(hdcPict, pict);
759 StretchBlt(hdc, dstX, dstY, dstWidth, dstHeight,
760 hdcPict, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
762 ExcludeClipRect(hdc, dstX, dstY, dstWidth+dstX, dstHeight+dstY);
767 FillRect(hdc, &pr, (HBRUSH)GetStockObject(WHITE_BRUSH));
768 SelectObject(hdc, GetStockObject(BLACK_BRUSH));
770 MoveToEx(hdc, bounds.left, bounds.top, NULL);
771 LineTo(hdc, bounds.left+width-1, bounds.top);
772 LineTo(hdc, bounds.left+width-1, bounds.top+height-1);
773 LineTo(hdc, bounds.left, bounds.top+height-1);
774 LineTo(hdc, bounds.left, bounds.top);
778 void VLCPlugin::onPositionChange(LPCRECT lprcPosRect, LPCRECT lprcClipRect)
780 RECT clipRect = *lprcClipRect;
781 RECT posRect = *lprcPosRect;
784 ** tell container that previous area needs redrawing
787 InvalidateRect(GetParent(_inplacewnd), &_posRect, TRUE);
790 ** record keeping of control geometry within container
796 ** convert posRect & clipRect to match control viewport coordinates
798 getViewportCoords(&posRect, &clipRect);
801 ** change in-place window geometry to match clipping region
803 MoveWindow(_inplacewnd,
806 clipRect.right-clipRect.left,
807 clipRect.bottom-clipRect.top,
811 ** change video window geometry to match object bounds within clipping region
813 MoveWindow(_videownd,
816 posRect.right-posRect.left,
817 posRect.bottom-posRect.top,
821 ** force a full refresh of control content
824 updateRect.left = -posRect.left;
825 updateRect.top = -posRect.top;
826 updateRect.right = posRect.right-posRect.left;
827 updateRect.bottom = posRect.bottom-posRect.top;
829 ValidateRect(_videownd, NULL);
830 InvalidateRect(_videownd, &updateRect, FALSE);
831 UpdateWindow(_videownd);
834 void VLCPlugin::firePropChangedEvent(DISPID dispid)
838 vlcConnectionPointContainer->firePropChangedEvent(dispid);
842 void VLCPlugin::fireOnPlayEvent(void)
846 DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
847 vlcConnectionPointContainer->fireEvent(DISPID_PlayEvent, &dispparamsNoArgs);
851 void VLCPlugin::fireOnPauseEvent(void)
855 DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
856 vlcConnectionPointContainer->fireEvent(DISPID_PauseEvent, &dispparamsNoArgs);
860 void VLCPlugin::fireOnStopEvent(void)
864 DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
865 vlcConnectionPointContainer->fireEvent(DISPID_StopEvent, &dispparamsNoArgs);