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);
255 VLCPlugin::~VLCPlugin()
257 vlcOleInPlaceObject->UIDeactivate();
258 vlcOleInPlaceObject->InPlaceDeactivate();
260 delete vlcViewObject;
262 delete vlcObjectSafety;
263 delete vlcConnectionPointContainer;
264 delete vlcProvideClassInfo;
265 delete vlcPersistPropertyBag;
266 delete vlcPersistStreamInit;
267 delete vlcPersistStorage;
268 delete vlcOleInPlaceActiveObject;
269 delete vlcOleInPlaceObject;
270 delete vlcOleControl;
279 STDMETHODIMP VLCPlugin::QueryInterface(REFIID riid, void **ppv)
284 if( IID_IUnknown == riid )
287 *ppv = reinterpret_cast<LPVOID>(this);
290 else if( IID_IOleObject == riid )
293 *ppv = reinterpret_cast<LPVOID>(vlcOleObject);
296 else if( IID_IOleControl == riid )
299 *ppv = reinterpret_cast<LPVOID>(vlcOleControl);
302 else if( IID_IOleWindow == riid )
305 *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
308 else if( IID_IOleInPlaceObject == riid )
311 *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
314 else if( IID_IOleInPlaceActiveObject == riid )
317 *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceActiveObject);
320 else if( IID_IPersist == riid )
323 *ppv = reinterpret_cast<LPVOID>(vlcPersistPropertyBag);
326 else if( IID_IPersistStreamInit == riid )
329 *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
332 else if( IID_IPersistStorage == riid )
335 *ppv = reinterpret_cast<LPVOID>(vlcPersistStorage);
338 else if( IID_IPersistPropertyBag == riid )
341 *ppv = reinterpret_cast<LPVOID>(vlcPersistPropertyBag);
344 else if( IID_IProvideClassInfo == riid )
347 *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
350 else if( IID_IProvideClassInfo2 == riid )
353 *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
356 else if( IID_IConnectionPointContainer == riid )
359 *ppv = reinterpret_cast<LPVOID>(vlcConnectionPointContainer);
362 else if( IID_IObjectSafety == riid )
365 *ppv = reinterpret_cast<LPVOID>(vlcObjectSafety);
368 else if( IID_IDispatch == riid )
371 *ppv = reinterpret_cast<LPVOID>(vlcControl);
374 else if( IID_IVLCControl == riid )
377 *ppv = reinterpret_cast<LPVOID>(vlcControl);
380 else if( IID_IViewObject == riid )
383 *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
389 return E_NOINTERFACE;
392 STDMETHODIMP_(ULONG) VLCPlugin::AddRef(void)
394 return InterlockedIncrement((LONG *)&_i_ref);
397 STDMETHODIMP_(ULONG) VLCPlugin::Release(void)
399 if( ! InterlockedDecrement((LONG *)&_i_ref) )
407 //////////////////////////////////////
410 ** we use an in-place child window to represent plugin viewport,
411 ** whose size is limited by the clipping rectangle
412 ** all drawing within this window must follow
413 ** cartesian coordinate system represented by _bounds.
416 void VLCPlugin::calcPositionChange(LPRECT lprPosRect, LPCRECT lprcClipRect)
418 _bounds.right = lprPosRect->right-lprPosRect->left;
420 if( lprcClipRect->left <= lprPosRect->left )
422 // left side is not clipped out
425 if( lprcClipRect->right >= lprPosRect->right )
427 // right side is not clipped out, no change
429 else if( lprcClipRect->right >= lprPosRect->left )
431 // right side is clipped out
432 lprPosRect->right = lprcClipRect->right;
436 // outside of clipping rectange, not visible
437 lprPosRect->right = lprPosRect->left;
442 // left side is clipped out
443 _bounds.left = lprPosRect->left-lprcClipRect->left;
444 _bounds.right += _bounds.left;
446 lprPosRect->left = lprcClipRect->left;
447 if( lprcClipRect->right >= lprPosRect->right )
449 // right side is not clipped out
453 // right side is clipped out
454 lprPosRect->right = lprcClipRect->right;
458 _bounds.bottom = lprPosRect->bottom-lprPosRect->top;
460 if( lprcClipRect->top <= lprPosRect->top )
462 // top side is not clipped out
465 if( lprcClipRect->bottom >= lprPosRect->bottom )
467 // bottom side is not clipped out, no change
469 else if( lprcClipRect->bottom >= lprPosRect->top )
471 // bottom side is clipped out
472 lprPosRect->bottom = lprcClipRect->bottom;
476 // outside of clipping rectange, not visible
477 lprPosRect->right = lprPosRect->left;
482 _bounds.top = lprPosRect->top-lprcClipRect->top;
483 _bounds.bottom += _bounds.top;
485 lprPosRect->top = lprcClipRect->top;
486 if( lprcClipRect->bottom >= lprPosRect->bottom )
488 // bottom side is not clipped out
492 // bottom side is clipped out
493 lprPosRect->bottom = lprcClipRect->bottom;
498 HRESULT VLCPlugin::onInit(BOOL isNew)
503 char *ppsz_argv[] = { "vlc", "-vvv", "--fast-mutex", "--win9x-cv-method=1" };
505 char *ppsz_argv[] = { "vlc", "-vv" };
508 DWORD i_type, i_data = MAX_PATH + 1;
509 char p_data[MAX_PATH + 1];
510 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
511 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
513 if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
514 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
516 if( i_type == REG_SZ )
518 strcat( p_data, "\\vlc" );
519 ppsz_argv[0] = p_data;
522 RegCloseKey( h_key );
526 ppsz_argv[0] = "C:\\cygwin\\home\\Damien_Fouilleul\\dev\\videolan\\vlc-trunk\\vlc";
529 _i_vlc = VLC_Create();
531 if( VLC_Init(_i_vlc, sizeof(ppsz_argv)/sizeof(char*), ppsz_argv) )
541 ** object has fully initialized,
542 ** try to activate in place if container is ready
544 LPOLECLIENTSITE pActiveSite;
546 if( SUCCEEDED(vlcOleObject->GetClientSite(&pActiveSite)) && (NULL != pActiveSite) )
548 vlcOleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, pActiveSite, 0, NULL, NULL);
549 pActiveSite->Release();
557 HRESULT VLCPlugin::onLoad(void)
560 ** object has fully initialized,
561 ** try to activate in place if container is ready
563 LPOLECLIENTSITE pActiveSite;
565 if( SUCCEEDED(vlcOleObject->GetClientSite(&pActiveSite)) && (NULL != pActiveSite) )
567 vlcOleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, pActiveSite, 0, NULL, NULL);
568 pActiveSite->Release();
573 HRESULT VLCPlugin::onClientSiteChanged(LPOLECLIENTSITE pActiveSite)
575 if( NULL != pActiveSite )
578 ** object is embedded in container
579 ** try to activate in place if it has initialized
583 vlcOleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, pActiveSite, 0, NULL, NULL);
589 HRESULT VLCPlugin::onClose(DWORD dwSaveOption)
593 if( isInPlaceActive() )
595 onInPlaceDeactivate();
605 BOOL VLCPlugin::isInPlaceActive(void)
607 return (NULL != _inplacewnd);
610 HRESULT VLCPlugin::onActivateInPlace(LPMSG lpMesg, HWND hwndParent, LPCRECT lprcPosRect, LPCRECT lprcClipRect)
612 RECT posRect = *lprcPosRect;
614 calcPositionChange(&posRect, lprcClipRect);
616 _inplacewnd = CreateWindow(_p_class->getInPlaceWndClassName(),
617 "VLC Plugin In-Place Window",
618 WS_CHILD|WS_CLIPCHILDREN|WS_TABSTOP,
621 posRect.right-posRect.left,
622 posRect.bottom-posRect.top,
625 _p_class->getHInstance(),
629 if( NULL == _inplacewnd )
632 SetWindowLongPtr(_inplacewnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
634 _videownd = CreateWindow(_p_class->getVideoWndClassName(),
635 "VLC Plugin Video Window",
636 WS_CHILD|WS_CLIPCHILDREN|WS_VISIBLE,
639 _bounds.right-_bounds.left,
640 _bounds.bottom-_bounds.top,
643 _p_class->getHInstance(),
647 if( NULL == _videownd )
649 DestroyWindow(_inplacewnd);
653 SetWindowLongPtr(_videownd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
656 ShowWindow(_inplacewnd, SW_SHOWNORMAL);
658 /* horrible cast there */
660 val.i_int = reinterpret_cast<int>(_videownd);
661 VLC_VariableSet(_i_vlc, "drawable", val);
663 if( NULL != _psz_src )
665 // add target to playlist
667 int cOptionsCount = 0;
671 cOptions[cOptionsCount++] = "loop";
673 VLC_AddTarget(_i_vlc, _psz_src, (const char **)&cOptions, cOptionsCount, PLAYLIST_APPEND, PLAYLIST_END);
684 HRESULT VLCPlugin::onInPlaceDeactivate(void)
689 DestroyWindow(_videownd);
691 DestroyWindow(_inplacewnd);
697 void VLCPlugin::setVisible(BOOL fVisible)
699 _b_visible = fVisible;
700 if( isInPlaceActive() )
701 ShowWindow(_inplacewnd, fVisible ? SW_SHOWNORMAL : SW_HIDE);
702 firePropChangedEvent(DISPID_Visible);
705 void VLCPlugin::setFocus(BOOL fFocus)
708 SetActiveWindow(_inplacewnd);
711 BOOL VLCPlugin::hasFocus(void)
713 return GetActiveWindow() == _inplacewnd;
716 void VLCPlugin::onPaint(HDC hdc, const RECT &bounds, const RECT &pr)
719 ** if VLC is playing, it may not display any VIDEO content
720 ** hence, draw control logo
722 int width = bounds.right-bounds.left;
723 int height = bounds.bottom-bounds.top;
725 HBITMAP pict = _p_class->getInPlacePict();
728 HDC hdcPict = CreateCompatibleDC(hdc);
729 if( NULL != hdcPict )
732 if( GetObject(pict, sizeof(BITMAPINFO), &bm) )
734 int dstWidth = bm.bmWidth;
735 if( dstWidth > width-4 )
738 int dstHeight = bm.bmHeight;
739 if( dstHeight > height-4 )
740 dstHeight = height-4;
742 int dstX = bounds.left+(width-dstWidth)/2;
743 int dstY = bounds.top+(height-dstHeight)/2;
745 SelectObject(hdcPict, pict);
746 StretchBlt(hdc, dstX, dstY, dstWidth, dstHeight,
747 hdcPict, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
749 ExcludeClipRect(hdc, dstX, dstY, dstWidth+dstX, dstHeight+dstY);
754 FillRect(hdc, &pr, (HBRUSH)GetStockObject(WHITE_BRUSH));
755 SelectObject(hdc, GetStockObject(BLACK_BRUSH));
757 MoveToEx(hdc, bounds.left, bounds.top, NULL);
758 LineTo(hdc, bounds.left+width-1, bounds.top);
759 LineTo(hdc, bounds.left+width-1, bounds.top+height-1);
760 LineTo(hdc, bounds.left, bounds.top+height-1);
761 LineTo(hdc, bounds.left, bounds.top);
764 void VLCPlugin::onPositionChange(LPCRECT lprcPosRect, LPCRECT lprcClipRect)
766 RECT posRect = *lprcPosRect;
768 calcPositionChange(&posRect, lprcClipRect);
771 ** change in-place window geometry to match clipping region
773 MoveWindow(_inplacewnd,
776 posRect.right-posRect.left,
777 posRect.bottom-posRect.top,
781 ** change video window geometry to match object bounds within clipping region
783 MoveWindow(_videownd,
786 _bounds.right-_bounds.left,
787 _bounds.bottom-_bounds.top,
792 updateRect.left = -_bounds.left;
793 updateRect.top = -_bounds.top;
794 updateRect.right = _bounds.right-_bounds.left;
795 updateRect.bottom = _bounds.bottom-_bounds.top;
797 ValidateRect(_videownd, NULL);
798 InvalidateRect(_videownd, &updateRect, FALSE);
799 UpdateWindow(_videownd);
802 void VLCPlugin::firePropChangedEvent(DISPID dispid)
806 vlcConnectionPointContainer->firePropChangedEvent(dispid);
810 void VLCPlugin::fireOnPlayEvent(void)
814 DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
815 vlcConnectionPointContainer->fireEvent(DISPID_PlayEvent, &dispparamsNoArgs);
819 void VLCPlugin::fireOnPauseEvent(void)
823 DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
824 vlcConnectionPointContainer->fireEvent(DISPID_PauseEvent, &dispparamsNoArgs);
828 void VLCPlugin::fireOnStopEvent(void)
832 DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
833 vlcConnectionPointContainer->fireEvent(DISPID_StopEvent, &dispparamsNoArgs);