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 63 PlayerChildWindow::PlayerChildWindow( Window* pParent ) : 64 SystemChildWindow( pParent, WB_CLIPCHILDREN ) 65 { 66 } 67 68 // --------------------------------------------------------------------- 69 70 PlayerChildWindow::~PlayerChildWindow() 71 { 72 } 73 74 // --------------------------------------------------------------------- 75 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 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 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 109 void PlayerChildWindow::KeyInput( const KeyEvent& rKEvt ) 110 { 111 SystemChildWindow::KeyInput( rKEvt ); 112 GetParent()->KeyInput( rKEvt ); 113 } 114 115 // --------------------------------------------------------------------- 116 117 void PlayerChildWindow::KeyUp( const KeyEvent& rKEvt ) 118 { 119 SystemChildWindow::KeyUp( rKEvt ); 120 GetParent()->KeyUp( rKEvt ); 121 } 122 123 // --------------------------------------------------------------------- 124 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 // --------------- 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 148 Window::~Window() 149 { 150 dispose(); 151 } 152 153 // ------------------------------------------------------------------------------ 154 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 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 287 void SAL_CALL Window::update() 288 throw( uno::RuntimeException ) 289 { 290 if( mpPlayerWindow ) 291 { 292 mpPlayerWindow->Invalidate( INVALIDATE_UPDATE ); 293 mpPlayerWindow->Sync(); 294 } 295 } 296 297 // ------------------------------------------------------------------------------ 298 299 sal_Bool SAL_CALL Window::setZoomLevel( media::ZoomLevel eZoomLevel ) 300 throw(uno::RuntimeException) 301 { 302 bool bRet = false; 303 304 if( ( media::ZoomLevel_NOT_AVAILABLE != meZoomLevel ) && 305 ( media::ZoomLevel_NOT_AVAILABLE != eZoomLevel ) ) 306 { 307 if( eZoomLevel != meZoomLevel ) 308 { 309 meZoomLevel = eZoomLevel; 310 implLayoutVideoWindow(); 311 } 312 313 bRet = true; 314 } 315 316 return( bRet ); 317 } 318 319 // ------------------------------------------------------------------------------ 320 321 media::ZoomLevel SAL_CALL Window::getZoomLevel() 322 throw( uno::RuntimeException ) 323 { 324 return( meZoomLevel ); 325 } 326 327 // ------------------------------------------------------------------------------ 328 329 void SAL_CALL Window::setPointerType( sal_Int32 nPointerType ) 330 throw( uno::RuntimeException ) 331 { 332 if( mpPlayerWindow ) 333 { 334 Pointer ePointer = POINTER_ARROW; 335 336 switch( nPointerType ) 337 { 338 case (awt::SystemPointer::CROSS) : 339 { 340 ePointer = POINTER_CROSS; 341 } 342 break; 343 344 case (awt::SystemPointer::HAND) : 345 { 346 ePointer = POINTER_HAND; 347 } 348 break; 349 350 case (awt::SystemPointer::MOVE) : 351 { 352 ePointer = POINTER_MOVE; 353 } 354 break; 355 356 case (awt::SystemPointer::WAIT) : 357 { 358 ePointer = POINTER_WAIT; 359 } 360 break; 361 362 default: 363 break; 364 } 365 366 mpPlayerWindow->SetPointer( ePointer ); 367 } 368 } 369 370 // ------------------------------------------------------------------------------ 371 372 void SAL_CALL Window::setPosSize( sal_Int32 nX, 373 sal_Int32 nY, 374 sal_Int32 nWidth, 375 sal_Int32 nHeight, 376 sal_Int16 /* Flags */ ) 377 throw( uno::RuntimeException ) 378 { 379 maWindowRect.X = nX; 380 maWindowRect.Y = nY; 381 maWindowRect.Width = nWidth; 382 maWindowRect.Height = nHeight; 383 384 implLayoutVideoWindow(); 385 } 386 387 // ------------------------------------------------------------------------------ 388 389 awt::Rectangle SAL_CALL Window::getPosSize() 390 throw( uno::RuntimeException ) 391 { 392 return( maWindowRect ); 393 } 394 395 // ------------------------------------------------------------------------------ 396 397 void SAL_CALL Window::setVisible( sal_Bool bVisible ) 398 throw( uno::RuntimeException ) 399 { 400 if( mpPlayerWindow ) 401 { 402 bVisible ? mpPlayerWindow->Show() : mpPlayerWindow->Hide(); 403 } 404 } 405 406 // ------------------------------------------------------------------------------ 407 408 void SAL_CALL Window::setEnable( sal_Bool bEnable ) 409 throw( uno::RuntimeException ) 410 { 411 if( mpPlayerWindow ) 412 { 413 mpPlayerWindow->Enable( bEnable ); 414 } 415 } 416 417 // ------------------------------------------------------------------------------ 418 419 void SAL_CALL Window::setFocus() 420 throw( uno::RuntimeException ) 421 { 422 if( mpPlayerWindow ) 423 { 424 mpPlayerWindow->GrabFocus(); 425 } 426 } 427 428 // ------------------------------------------------------------------------------ 429 430 void SAL_CALL Window::addWindowListener( const uno::Reference< awt::XWindowListener >& xListener ) 431 throw( uno::RuntimeException ) 432 { 433 maListeners.addInterface( getCppuType( &xListener ), xListener ); 434 } 435 436 // ------------------------------------------------------------------------------ 437 438 void SAL_CALL Window::removeWindowListener( const uno::Reference< awt::XWindowListener >& xListener ) 439 throw( uno::RuntimeException ) 440 { 441 maListeners.removeInterface( getCppuType( &xListener ), xListener ); 442 } 443 444 // ------------------------------------------------------------------------------ 445 446 void SAL_CALL Window::addFocusListener( const uno::Reference< awt::XFocusListener >& xListener ) 447 throw( uno::RuntimeException ) 448 { 449 maListeners.addInterface( getCppuType( &xListener ), xListener ); 450 } 451 452 // ------------------------------------------------------------------------------ 453 454 void SAL_CALL Window::removeFocusListener( const uno::Reference< awt::XFocusListener >& xListener ) 455 throw( uno::RuntimeException ) 456 { 457 maListeners.removeInterface( getCppuType( &xListener ), xListener ); 458 } 459 460 // ------------------------------------------------------------------------------ 461 462 void SAL_CALL Window::addKeyListener( const uno::Reference< awt::XKeyListener >& xListener ) 463 throw( uno::RuntimeException ) 464 { 465 maListeners.addInterface( getCppuType( &xListener ), xListener ); 466 } 467 468 // ------------------------------------------------------------------------------ 469 470 void SAL_CALL Window::removeKeyListener( const uno::Reference< awt::XKeyListener >& xListener ) 471 throw( uno::RuntimeException ) 472 { 473 maListeners.removeInterface( getCppuType( &xListener ), xListener ); 474 } 475 476 // ------------------------------------------------------------------------------ 477 478 void SAL_CALL Window::addMouseListener( const uno::Reference< awt::XMouseListener >& xListener ) 479 throw( uno::RuntimeException ) 480 { 481 maListeners.addInterface( getCppuType( &xListener ), xListener ); 482 } 483 484 // ------------------------------------------------------------------------------ 485 486 void SAL_CALL Window::removeMouseListener( const uno::Reference< awt::XMouseListener >& xListener ) 487 throw( uno::RuntimeException ) 488 { 489 maListeners.removeInterface( getCppuType( &xListener ), xListener ); 490 } 491 492 // ------------------------------------------------------------------------------ 493 494 void SAL_CALL Window::addMouseMotionListener( const uno::Reference< awt::XMouseMotionListener >& xListener ) 495 throw( uno::RuntimeException ) 496 { 497 maListeners.addInterface( getCppuType( &xListener ), xListener ); 498 } 499 500 // ------------------------------------------------------------------------------ 501 502 void SAL_CALL Window::removeMouseMotionListener( const uno::Reference< awt::XMouseMotionListener >& xListener ) 503 throw( uno::RuntimeException ) 504 { 505 maListeners.removeInterface( getCppuType( &xListener ), xListener ); 506 } 507 508 // ------------------------------------------------------------------------------ 509 510 void SAL_CALL Window::addPaintListener( const uno::Reference< awt::XPaintListener >& xListener ) 511 throw( uno::RuntimeException ) 512 { 513 maListeners.addInterface( getCppuType( &xListener ), xListener ); 514 } 515 516 // ------------------------------------------------------------------------------ 517 518 void SAL_CALL Window::removePaintListener( const uno::Reference< awt::XPaintListener >& xListener ) 519 throw( uno::RuntimeException ) 520 { 521 maListeners.removeInterface( getCppuType( &xListener ), xListener ); 522 } 523 524 // ------------------------------------------------------------------------------ 525 526 void SAL_CALL Window::dispose() 527 throw( uno::RuntimeException ) 528 { 529 delete mpPlayerWindow; 530 mpPlayerWindow = NULL; 531 } 532 533 // ------------------------------------------------------------------------------ 534 535 void SAL_CALL Window::addEventListener( const uno::Reference< lang::XEventListener >& xListener ) 536 throw( uno::RuntimeException ) 537 { 538 maListeners.addInterface( getCppuType( &xListener ), xListener ); 539 } 540 541 // ------------------------------------------------------------------------------ 542 543 void SAL_CALL Window::removeEventListener( const uno::Reference< lang::XEventListener >& 544 xListener ) 545 throw( uno::RuntimeException ) 546 { 547 maListeners.removeInterface( getCppuType( &xListener ), xListener ); 548 } 549 550 // ------------------------------------------------------------------------------ 551 552 void Window::fireMousePressedEvent( const ::com::sun::star::awt::MouseEvent& rEvt ) 553 { 554 ::cppu::OInterfaceContainerHelper* pContainer = 555 maListeners.getContainer( getCppuType( ( uno::Reference< awt::XMouseListener >* ) 0 ) ); 556 557 if( pContainer ) 558 { 559 ::cppu::OInterfaceIteratorHelper aIter( *pContainer ); 560 561 while( aIter.hasMoreElements() ) 562 { 563 uno::Reference< awt::XMouseListener >( aIter.next(), 564 uno::UNO_QUERY )->mousePressed( rEvt ); 565 } 566 } 567 } 568 569 // ----------------------------------------------------------------------------- 570 571 void Window::fireMouseReleasedEvent( const ::com::sun::star::awt::MouseEvent& rEvt ) 572 { 573 ::cppu::OInterfaceContainerHelper* pContainer = 574 maListeners.getContainer( getCppuType( ( uno::Reference< awt::XMouseListener >* ) 0 ) ); 575 576 if( pContainer ) 577 { 578 ::cppu::OInterfaceIteratorHelper aIter( *pContainer ); 579 580 while( aIter.hasMoreElements() ) 581 { 582 uno::Reference< awt::XMouseListener >( aIter.next(), 583 uno::UNO_QUERY )->mouseReleased( rEvt ); 584 } 585 } 586 } 587 588 // ----------------------------------------------------------------------------- 589 590 void Window::fireMouseMovedEvent( const ::com::sun::star::awt::MouseEvent& rEvt ) 591 { 592 ::cppu::OInterfaceContainerHelper* pContainer = 593 maListeners.getContainer( getCppuType( ( uno::Reference< awt::XMouseMotionListener >* ) 594 0 ) ); 595 596 if( pContainer ) 597 { 598 ::cppu::OInterfaceIteratorHelper aIter( *pContainer ); 599 600 while( aIter.hasMoreElements() ) 601 { 602 uno::Reference< awt::XMouseMotionListener >( aIter.next(), 603 uno::UNO_QUERY )->mouseMoved( rEvt ); 604 } 605 } 606 } 607 608 // ----------------------------------------------------------------------------- 609 610 void Window::fireSetFocusEvent( const ::com::sun::star::awt::FocusEvent& rEvt ) 611 { 612 ::cppu::OInterfaceContainerHelper* pContainer = 613 maListeners.getContainer( getCppuType( ( uno::Reference< awt::XFocusListener >* ) 0 ) ); 614 615 if( pContainer ) 616 { 617 ::cppu::OInterfaceIteratorHelper aIter( *pContainer ); 618 619 while( aIter.hasMoreElements() ) 620 { 621 uno::Reference< awt::XFocusListener >( aIter.next(), 622 uno::UNO_QUERY )->focusGained( rEvt ); 623 } 624 } 625 } 626 627 // ------------------------------------------------------------------------------ 628 629 long Window::getXWindowHandle() const 630 { 631 return( mpPlayerWindow ? mpPlayerWindow->GetSystemData()->aWindow : 0 ); 632 } 633 634 635 // ------------------------------------------------------------------------------ 636 637 ::rtl::OUString SAL_CALL Window::getImplementationName() 638 throw( uno::RuntimeException ) 639 { 640 return( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( 641 AVMEDIA_GSTREAMER_WINDOW_IMPLEMENTATIONNAME ) ) ); 642 } 643 644 // ------------------------------------------------------------------------------ 645 646 sal_Bool SAL_CALL Window::supportsService( const ::rtl::OUString& ServiceName ) 647 throw( uno::RuntimeException ) 648 { 649 return( ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( 650 AVMEDIA_GSTREAMER_WINDOW_SERVICENAME ) ) ); 651 } 652 653 // ------------------------------------------------------------------------------ 654 655 uno::Sequence< ::rtl::OUString > SAL_CALL Window::getSupportedServiceNames() 656 throw( uno::RuntimeException ) 657 { 658 uno::Sequence< ::rtl::OUString > aRet( 1 ); 659 aRet[ 0 ] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( AVMEDIA_GSTREAMER_WINDOW_SERVICENAME ) ); 660 661 return( aRet ); 662 } 663 } // namespace gst 664 } // namespace avmedia 665