1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 #include <tools/prex.h>
25 #include <tools/postx.h>
26
27 #include <com/sun/star/awt/SystemPointer.hdl>
28
29 #include "gstwindow.hxx"
30 #include "gstplayer.hxx"
31 #include <vcl/syschild.hxx>
32 #include <vcl/sysdata.hxx>
33
34 using namespace ::com::sun::star;
35
36 namespace avmedia
37 {
38 namespace gst
39 {
40 // ---------------------
41 // - PlayerChildWindow -
42 // ---------------------
43
44 class PlayerChildWindow : public SystemChildWindow
45 {
46 public:
47
48 PlayerChildWindow( Window* pParent );
49 ~PlayerChildWindow();
50
51 protected:
52
53 virtual void MouseMove( const MouseEvent& rMEvt );
54 virtual void MouseButtonDown( const MouseEvent& rMEvt );
55 virtual void MouseButtonUp( const MouseEvent& rMEvt );
56 virtual void KeyInput( const KeyEvent& rKEvt );
57 virtual void KeyUp( const KeyEvent& rKEvt );
58 virtual void Command( const CommandEvent& rCEvt );
59 };
60
61 // ---------------------------------------------------------------------
62
PlayerChildWindow(Window * pParent)63 PlayerChildWindow::PlayerChildWindow( Window* pParent ) :
64 SystemChildWindow( pParent, WB_CLIPCHILDREN )
65 {
66 }
67
68 // ---------------------------------------------------------------------
69
~PlayerChildWindow()70 PlayerChildWindow::~PlayerChildWindow()
71 {
72 }
73
74 // ---------------------------------------------------------------------
75
MouseMove(const MouseEvent & rMEvt)76 void PlayerChildWindow::MouseMove( const MouseEvent& rMEvt )
77 {
78 const MouseEvent aTransformedEvent( GetParent()->ScreenToOutputPixel( OutputToScreenPixel( rMEvt.GetPosPixel() ) ),
79 rMEvt.GetClicks(), rMEvt.GetMode(), rMEvt.GetButtons(), rMEvt.GetModifier() );
80
81 SystemChildWindow::MouseMove( rMEvt );
82 GetParent()->MouseMove( aTransformedEvent );
83 }
84
85 // ---------------------------------------------------------------------
86
MouseButtonDown(const MouseEvent & rMEvt)87 void PlayerChildWindow::MouseButtonDown( const MouseEvent& rMEvt )
88 {
89 const MouseEvent aTransformedEvent( GetParent()->ScreenToOutputPixel( OutputToScreenPixel( rMEvt.GetPosPixel() ) ),
90 rMEvt.GetClicks(), rMEvt.GetMode(), rMEvt.GetButtons(), rMEvt.GetModifier() );
91
92 SystemChildWindow::MouseButtonDown( rMEvt );
93 GetParent()->MouseButtonDown( aTransformedEvent );
94 }
95
96 // ---------------------------------------------------------------------
97
MouseButtonUp(const MouseEvent & rMEvt)98 void PlayerChildWindow::MouseButtonUp( const MouseEvent& rMEvt )
99 {
100 const MouseEvent aTransformedEvent( GetParent()->ScreenToOutputPixel( OutputToScreenPixel( rMEvt.GetPosPixel() ) ),
101 rMEvt.GetClicks(), rMEvt.GetMode(), rMEvt.GetButtons(), rMEvt.GetModifier() );
102
103 SystemChildWindow::MouseButtonUp( rMEvt );
104 GetParent()->MouseButtonUp( aTransformedEvent );
105 }
106
107 // ---------------------------------------------------------------------
108
KeyInput(const KeyEvent & rKEvt)109 void PlayerChildWindow::KeyInput( const KeyEvent& rKEvt )
110 {
111 SystemChildWindow::KeyInput( rKEvt );
112 GetParent()->KeyInput( rKEvt );
113 }
114
115 // ---------------------------------------------------------------------
116
KeyUp(const KeyEvent & rKEvt)117 void PlayerChildWindow::KeyUp( const KeyEvent& rKEvt )
118 {
119 SystemChildWindow::KeyUp( rKEvt );
120 GetParent()->KeyUp( rKEvt );
121 }
122
123 // ---------------------------------------------------------------------
124
Command(const CommandEvent & rCEvt)125 void PlayerChildWindow::Command( const CommandEvent& rCEvt )
126 {
127 const CommandEvent aTransformedEvent( GetParent()->ScreenToOutputPixel( OutputToScreenPixel( rCEvt.GetMousePosPixel() ) ),
128 rCEvt.GetCommand(), rCEvt.IsMouseEvent(), rCEvt.GetData() );
129
130 SystemChildWindow::Command( rCEvt );
131 GetParent()->Command( aTransformedEvent );
132 }
133
134 // ---------------
135 // - Window -
136 // ---------------
Window(::avmedia::gst::Player & rPlayer)137 Window::Window( ::avmedia::gst::Player& rPlayer ) :
138 maListeners( maMutex ),
139 maWindowRect( 0, 0, 0, 0 ),
140 mrPlayer( rPlayer ),
141 mpPlayerWindow( NULL ),
142 meZoomLevel( media::ZoomLevel_NOT_AVAILABLE ),
143 mnPointerType( awt::SystemPointer::ARROW )
144 {}
145
146 // ------------------------------------------------------------------------------
147
~Window()148 Window::~Window()
149 {
150 dispose();
151 }
152
153 // ------------------------------------------------------------------------------
154
implLayoutVideoWindow()155 void Window::implLayoutVideoWindow()
156 {
157 if( mpPlayerWindow && ( media::ZoomLevel_NOT_AVAILABLE != meZoomLevel ) )
158 {
159 awt::Size aPrefSize( mrPlayer.getPreferredPlayerWindowSize() );
160 awt::Rectangle aRect = getPosSize();
161 int nW = aRect.Width, nH = aRect.Height;
162 int nVideoW = nW, nVideoH = nH;
163 int nX = 0, nY = 0, nWidth = 0, nHeight = 0;
164 bool bDone = false, bZoom = false;
165
166 if( media::ZoomLevel_ORIGINAL == meZoomLevel )
167 {
168 bZoom = true;
169 }
170 else if( media::ZoomLevel_ZOOM_1_TO_4 == meZoomLevel )
171 {
172 aPrefSize.Width >>= 2;
173 aPrefSize.Height >>= 2;
174 bZoom = true;
175 }
176 else if( media::ZoomLevel_ZOOM_1_TO_2 == meZoomLevel )
177 {
178 aPrefSize.Width >>= 1;
179 aPrefSize.Height >>= 1;
180 bZoom = true;
181 }
182 else if( media::ZoomLevel_ZOOM_2_TO_1 == meZoomLevel )
183 {
184 aPrefSize.Width <<= 1;
185 aPrefSize.Height <<= 1;
186 bZoom = true;
187 }
188 else if( media::ZoomLevel_ZOOM_4_TO_1 == meZoomLevel )
189 {
190 aPrefSize.Width <<= 2;
191 aPrefSize.Height <<= 2;
192 bZoom = true;
193 }
194 else if( media::ZoomLevel_FIT_TO_WINDOW == meZoomLevel )
195 {
196 nWidth = nVideoW;
197 nHeight = nVideoH;
198 bDone = true;
199 }
200
201 if( bZoom )
202 {
203 if( ( aPrefSize.Width <= nVideoW ) && ( aPrefSize.Height <= nVideoH ) )
204 {
205 nX = ( nVideoW - aPrefSize.Width ) >> 1;
206 nY = ( nVideoH - aPrefSize.Height ) >> 1;
207 nWidth = aPrefSize.Width;
208 nHeight = aPrefSize.Height;
209 bDone = true;
210 }
211 }
212
213 if( !bDone )
214 {
215 if( ( aPrefSize.Width > 0 ) && ( aPrefSize.Height > 0 ) && ( nVideoW > 0 ) && ( nVideoH > 0 ) )
216 {
217 double fPrefWH = (double) aPrefSize.Width / aPrefSize.Height;
218
219 if( fPrefWH < ( (double) nVideoW / nVideoH ) )
220 {
221 nVideoW = (int) ( nVideoH * fPrefWH );
222 }
223 else
224 {
225 nVideoH = (int) ( nVideoW / fPrefWH );
226 }
227
228 nX = ( nW - nVideoW ) >> 1;
229 nY = ( nH - nVideoH ) >> 1;
230 nWidth = nVideoW;
231 nHeight = nVideoH;
232 }
233 else
234 {
235 nX = nY = nWidth = nHeight = 0;
236 }
237 }
238
239 mpPlayerWindow->SetPosSizePixel( Point( nX, nY ), Size( nWidth, nHeight ) );
240 }
241 }
242
243 // ------------------------------------------------------------------------------
244
create(const uno::Sequence<uno::Any> & rArguments)245 bool Window::create( const uno::Sequence< uno::Any >& rArguments )
246 {
247 OSL_ASSERT( !mpPlayerWindow );
248 OSL_ENSURE( rArguments.getLength() > 2,
249 "::avmedia::gst::Windiow::create: GStreamer backend needs \
250 valid parent SystemChildWindow pointer (sal_IntPtr) as third Any \
251 value in sequence of arguments" );
252
253 bool bRet = false;
254
255 if( !mpPlayerWindow && ( rArguments.getLength() > 2 ) )
256 {
257 SystemChildWindow* pParentWindow = NULL;
258 sal_IntPtr pIntPtr = 0;
259
260 // we are not interested in the first argument, which is a pure X11 window handle;
261 // this backend relies on a SystemChildWindow as parent for the PlayerWindow, given
262 // as third Sal_IntPtr value within the argument Sequence
263 rArguments[ 1 ] >>= maWindowRect;
264
265 rArguments[ 2 ] >>= pIntPtr;
266 pParentWindow = reinterpret_cast< SystemChildWindow* >( pIntPtr );
267
268 if( pParentWindow )
269 {
270 meZoomLevel = media::ZoomLevel_FIT_TO_WINDOW;
271
272 mpPlayerWindow = new PlayerChildWindow( pParentWindow );
273 mpPlayerWindow->SetBackground( Color( COL_BLACK ) );
274 mpPlayerWindow->SetPosSizePixel( Point( maWindowRect.X, maWindowRect.Y ),
275 Size( maWindowRect.Width, maWindowRect.Height ) );
276 mpPlayerWindow->Show();
277
278 bRet = true;
279 }
280 }
281
282 return( bRet );
283 }
284
285 // ------------------------------------------------------------------------------
286
update()287 void SAL_CALL Window::update()
288 {
289 if( mpPlayerWindow )
290 {
291 mpPlayerWindow->Invalidate( INVALIDATE_UPDATE );
292 mpPlayerWindow->Sync();
293 }
294 }
295
296 // ------------------------------------------------------------------------------
297
setZoomLevel(media::ZoomLevel eZoomLevel)298 sal_Bool SAL_CALL Window::setZoomLevel( media::ZoomLevel eZoomLevel )
299 {
300 bool bRet = false;
301
302 if( ( media::ZoomLevel_NOT_AVAILABLE != meZoomLevel ) &&
303 ( media::ZoomLevel_NOT_AVAILABLE != eZoomLevel ) )
304 {
305 if( eZoomLevel != meZoomLevel )
306 {
307 meZoomLevel = eZoomLevel;
308 implLayoutVideoWindow();
309 }
310
311 bRet = true;
312 }
313
314 return( bRet );
315 }
316
317 // ------------------------------------------------------------------------------
318
getZoomLevel()319 media::ZoomLevel SAL_CALL Window::getZoomLevel()
320 {
321 return( meZoomLevel );
322 }
323
324 // ------------------------------------------------------------------------------
325
setPointerType(sal_Int32 nPointerType)326 void SAL_CALL Window::setPointerType( sal_Int32 nPointerType )
327 {
328 if( mpPlayerWindow )
329 {
330 Pointer ePointer = POINTER_ARROW;
331
332 switch( nPointerType )
333 {
334 case (awt::SystemPointer::CROSS) :
335 {
336 ePointer = POINTER_CROSS;
337 }
338 break;
339
340 case (awt::SystemPointer::HAND) :
341 {
342 ePointer = POINTER_HAND;
343 }
344 break;
345
346 case (awt::SystemPointer::MOVE) :
347 {
348 ePointer = POINTER_MOVE;
349 }
350 break;
351
352 case (awt::SystemPointer::WAIT) :
353 {
354 ePointer = POINTER_WAIT;
355 }
356 break;
357
358 default:
359 break;
360 }
361
362 mpPlayerWindow->SetPointer( ePointer );
363 }
364 }
365
366 // ------------------------------------------------------------------------------
367
setPosSize(sal_Int32 nX,sal_Int32 nY,sal_Int32 nWidth,sal_Int32 nHeight,sal_Int16)368 void SAL_CALL Window::setPosSize( sal_Int32 nX,
369 sal_Int32 nY,
370 sal_Int32 nWidth,
371 sal_Int32 nHeight,
372 sal_Int16 /* Flags */ )
373 {
374 maWindowRect.X = nX;
375 maWindowRect.Y = nY;
376 maWindowRect.Width = nWidth;
377 maWindowRect.Height = nHeight;
378
379 implLayoutVideoWindow();
380 }
381
382 // ------------------------------------------------------------------------------
383
getPosSize()384 awt::Rectangle SAL_CALL Window::getPosSize()
385 {
386 return( maWindowRect );
387 }
388
389 // ------------------------------------------------------------------------------
390
setVisible(sal_Bool bVisible)391 void SAL_CALL Window::setVisible( sal_Bool bVisible )
392 {
393 if( mpPlayerWindow )
394 {
395 bVisible ? mpPlayerWindow->Show() : mpPlayerWindow->Hide();
396 }
397 }
398
399 // ------------------------------------------------------------------------------
400
setEnable(sal_Bool bEnable)401 void SAL_CALL Window::setEnable( sal_Bool bEnable )
402 {
403 if( mpPlayerWindow )
404 {
405 mpPlayerWindow->Enable( bEnable );
406 }
407 }
408
409 // ------------------------------------------------------------------------------
410
setFocus()411 void SAL_CALL Window::setFocus()
412 {
413 if( mpPlayerWindow )
414 {
415 mpPlayerWindow->GrabFocus();
416 }
417 }
418
419 // ------------------------------------------------------------------------------
420
addWindowListener(const uno::Reference<awt::XWindowListener> & xListener)421 void SAL_CALL Window::addWindowListener( const uno::Reference< awt::XWindowListener >& xListener )
422 {
423 maListeners.addInterface( getCppuType( &xListener ), xListener );
424 }
425
426 // ------------------------------------------------------------------------------
427
removeWindowListener(const uno::Reference<awt::XWindowListener> & xListener)428 void SAL_CALL Window::removeWindowListener( const uno::Reference< awt::XWindowListener >& xListener )
429 {
430 maListeners.removeInterface( getCppuType( &xListener ), xListener );
431 }
432
433 // ------------------------------------------------------------------------------
434
addFocusListener(const uno::Reference<awt::XFocusListener> & xListener)435 void SAL_CALL Window::addFocusListener( const uno::Reference< awt::XFocusListener >& xListener )
436 {
437 maListeners.addInterface( getCppuType( &xListener ), xListener );
438 }
439
440 // ------------------------------------------------------------------------------
441
removeFocusListener(const uno::Reference<awt::XFocusListener> & xListener)442 void SAL_CALL Window::removeFocusListener( const uno::Reference< awt::XFocusListener >& xListener )
443 {
444 maListeners.removeInterface( getCppuType( &xListener ), xListener );
445 }
446
447 // ------------------------------------------------------------------------------
448
addKeyListener(const uno::Reference<awt::XKeyListener> & xListener)449 void SAL_CALL Window::addKeyListener( const uno::Reference< awt::XKeyListener >& xListener )
450 {
451 maListeners.addInterface( getCppuType( &xListener ), xListener );
452 }
453
454 // ------------------------------------------------------------------------------
455
removeKeyListener(const uno::Reference<awt::XKeyListener> & xListener)456 void SAL_CALL Window::removeKeyListener( const uno::Reference< awt::XKeyListener >& xListener )
457 {
458 maListeners.removeInterface( getCppuType( &xListener ), xListener );
459 }
460
461 // ------------------------------------------------------------------------------
462
addMouseListener(const uno::Reference<awt::XMouseListener> & xListener)463 void SAL_CALL Window::addMouseListener( const uno::Reference< awt::XMouseListener >& xListener )
464 {
465 maListeners.addInterface( getCppuType( &xListener ), xListener );
466 }
467
468 // ------------------------------------------------------------------------------
469
removeMouseListener(const uno::Reference<awt::XMouseListener> & xListener)470 void SAL_CALL Window::removeMouseListener( const uno::Reference< awt::XMouseListener >& xListener )
471 {
472 maListeners.removeInterface( getCppuType( &xListener ), xListener );
473 }
474
475 // ------------------------------------------------------------------------------
476
addMouseMotionListener(const uno::Reference<awt::XMouseMotionListener> & xListener)477 void SAL_CALL Window::addMouseMotionListener( const uno::Reference< awt::XMouseMotionListener >& xListener )
478 {
479 maListeners.addInterface( getCppuType( &xListener ), xListener );
480 }
481
482 // ------------------------------------------------------------------------------
483
removeMouseMotionListener(const uno::Reference<awt::XMouseMotionListener> & xListener)484 void SAL_CALL Window::removeMouseMotionListener( const uno::Reference< awt::XMouseMotionListener >& xListener )
485 {
486 maListeners.removeInterface( getCppuType( &xListener ), xListener );
487 }
488
489 // ------------------------------------------------------------------------------
490
addPaintListener(const uno::Reference<awt::XPaintListener> & xListener)491 void SAL_CALL Window::addPaintListener( const uno::Reference< awt::XPaintListener >& xListener )
492 {
493 maListeners.addInterface( getCppuType( &xListener ), xListener );
494 }
495
496 // ------------------------------------------------------------------------------
497
removePaintListener(const uno::Reference<awt::XPaintListener> & xListener)498 void SAL_CALL Window::removePaintListener( const uno::Reference< awt::XPaintListener >& xListener )
499 {
500 maListeners.removeInterface( getCppuType( &xListener ), xListener );
501 }
502
503 // ------------------------------------------------------------------------------
504
dispose()505 void SAL_CALL Window::dispose()
506 {
507 delete mpPlayerWindow;
508 mpPlayerWindow = NULL;
509 }
510
511 // ------------------------------------------------------------------------------
512
addEventListener(const uno::Reference<lang::XEventListener> & xListener)513 void SAL_CALL Window::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
514 {
515 maListeners.addInterface( getCppuType( &xListener ), xListener );
516 }
517
518 // ------------------------------------------------------------------------------
519
removeEventListener(const uno::Reference<lang::XEventListener> & xListener)520 void SAL_CALL Window::removeEventListener( const uno::Reference< lang::XEventListener >&
521 xListener )
522 {
523 maListeners.removeInterface( getCppuType( &xListener ), xListener );
524 }
525
526 // ------------------------------------------------------------------------------
527
fireMousePressedEvent(const::com::sun::star::awt::MouseEvent & rEvt)528 void Window::fireMousePressedEvent( const ::com::sun::star::awt::MouseEvent& rEvt )
529 {
530 ::cppu::OInterfaceContainerHelper* pContainer =
531 maListeners.getContainer( getCppuType( ( uno::Reference< awt::XMouseListener >* ) 0 ) );
532
533 if( pContainer )
534 {
535 ::cppu::OInterfaceIteratorHelper aIter( *pContainer );
536
537 while( aIter.hasMoreElements() )
538 {
539 uno::Reference< awt::XMouseListener >( aIter.next(),
540 uno::UNO_QUERY )->mousePressed( rEvt );
541 }
542 }
543 }
544
545 // -----------------------------------------------------------------------------
546
fireMouseReleasedEvent(const::com::sun::star::awt::MouseEvent & rEvt)547 void Window::fireMouseReleasedEvent( const ::com::sun::star::awt::MouseEvent& rEvt )
548 {
549 ::cppu::OInterfaceContainerHelper* pContainer =
550 maListeners.getContainer( getCppuType( ( uno::Reference< awt::XMouseListener >* ) 0 ) );
551
552 if( pContainer )
553 {
554 ::cppu::OInterfaceIteratorHelper aIter( *pContainer );
555
556 while( aIter.hasMoreElements() )
557 {
558 uno::Reference< awt::XMouseListener >( aIter.next(),
559 uno::UNO_QUERY )->mouseReleased( rEvt );
560 }
561 }
562 }
563
564 // -----------------------------------------------------------------------------
565
fireMouseMovedEvent(const::com::sun::star::awt::MouseEvent & rEvt)566 void Window::fireMouseMovedEvent( const ::com::sun::star::awt::MouseEvent& rEvt )
567 {
568 ::cppu::OInterfaceContainerHelper* pContainer =
569 maListeners.getContainer( getCppuType( ( uno::Reference< awt::XMouseMotionListener >* )
570 0 ) );
571
572 if( pContainer )
573 {
574 ::cppu::OInterfaceIteratorHelper aIter( *pContainer );
575
576 while( aIter.hasMoreElements() )
577 {
578 uno::Reference< awt::XMouseMotionListener >( aIter.next(),
579 uno::UNO_QUERY )->mouseMoved( rEvt );
580 }
581 }
582 }
583
584 // -----------------------------------------------------------------------------
585
fireSetFocusEvent(const::com::sun::star::awt::FocusEvent & rEvt)586 void Window::fireSetFocusEvent( const ::com::sun::star::awt::FocusEvent& rEvt )
587 {
588 ::cppu::OInterfaceContainerHelper* pContainer =
589 maListeners.getContainer( getCppuType( ( uno::Reference< awt::XFocusListener >* ) 0 ) );
590
591 if( pContainer )
592 {
593 ::cppu::OInterfaceIteratorHelper aIter( *pContainer );
594
595 while( aIter.hasMoreElements() )
596 {
597 uno::Reference< awt::XFocusListener >( aIter.next(),
598 uno::UNO_QUERY )->focusGained( rEvt );
599 }
600 }
601 }
602
603 // ------------------------------------------------------------------------------
604
getXWindowHandle() const605 long Window::getXWindowHandle() const
606 {
607 return( mpPlayerWindow ? mpPlayerWindow->GetSystemData()->aWindow : 0 );
608 }
609
610
611 // ------------------------------------------------------------------------------
612
getImplementationName()613 ::rtl::OUString SAL_CALL Window::getImplementationName()
614 {
615 return( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
616 AVMEDIA_GSTREAMER_WINDOW_IMPLEMENTATIONNAME ) ) );
617 }
618
619 // ------------------------------------------------------------------------------
620
supportsService(const::rtl::OUString & ServiceName)621 sal_Bool SAL_CALL Window::supportsService( const ::rtl::OUString& ServiceName )
622 {
623 return( ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
624 AVMEDIA_GSTREAMER_WINDOW_SERVICENAME ) ) );
625 }
626
627 // ------------------------------------------------------------------------------
628
getSupportedServiceNames()629 uno::Sequence< ::rtl::OUString > SAL_CALL Window::getSupportedServiceNames()
630 {
631 uno::Sequence< ::rtl::OUString > aRet( 1 );
632 aRet[ 0 ] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( AVMEDIA_GSTREAMER_WINDOW_SERVICENAME ) );
633
634 return( aRet );
635 }
636 } // namespace gst
637 } // namespace avmedia
638