]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/main_interface.cpp
Qt4: fix race in requestVideo and simplify
[vlc] / modules / gui / qt4 / main_interface.cpp
index e48af0d0d74796e98ccefbb73c4aca99417a2e8d..715d2fe0674893c9a049eef9e3df7bb0c0cbb75f 100644 (file)
@@ -61,6 +61,7 @@
 #include <assert.h>
 
 #include <vlc_keys.h> /* Wheel event */
+#include <vlc_vout_window.h>
 #include <vlc_vout.h>
 
 /* Callback prototypes */
@@ -84,6 +85,7 @@ MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
     cryptedLabel         = NULL;
     controls             = NULL;
     inputC               = NULL;
+    b_shouldHide         = false;
 
     bgWasVisible         = false;
     i_bg_height          = 0;
@@ -141,6 +143,8 @@ MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
      * Menu Bar *
      ************/
     QVLCMenu::createMenuBar( this, p_intf );
+    CONNECT( THEMIM->getIM(), voutListChanged( vout_thread_t **, int ),
+             this, destroyPopupMenu() );
 
 #if 0
     /* Create a Dock to get the playlist */
@@ -159,6 +163,11 @@ MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
      ********************/
     MainInputManager::getInstance( p_intf );
 
+    /*********************************
+     * Create the Systray Management *
+     *********************************/
+    initSystray();
+
     /**************************
      * Various CONNECTs on IM *
      **************************/
@@ -170,19 +179,19 @@ MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
      * them down.
      */
     /* Naming in the controller statusbar */
-    CONNECT( THEMIM->getIM(), nameChanged( QString ),
-             this, setName( QString ) );
+    CONNECT( THEMIM->getIM(), nameChanged( const QString& ),
+             this, setName( const QString& ) );
     /* and in the systray */
     if( sysTray )
     {
-        CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
-                 updateSystrayTooltipName( QString ) );
+        CONNECT( THEMIM->getIM(), nameChanged( const QString& ), this,
+                 updateSystrayTooltipName( const QString& ) );
     }
     /* and in the title of the controller */
     if( config_GetInt( p_intf, "qt-name-in-title" ) )
     {
-        CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
-             setVLCWindowsTitle( QString ) );
+        CONNECT( THEMIM->getIM(), nameChanged( const QString& ), this,
+             setVLCWindowsTitle( const QString& ) );
     }
 
     /**
@@ -195,9 +204,8 @@ MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
                  this, updateSystrayTooltipStatus( int ) );
     }
 
-    /* END CONNECTS ON IM */
 
-    dialogHandler = new DialogHandler (p_intf);
+    /* END CONNECTS ON IM */
 
     /************
      * Callbacks
@@ -208,18 +216,18 @@ MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
     var_AddCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
 
 
-    /* VideoWidget connects to avoid different threads speaking to each other */
+    /* VideoWidget connects for asynchronous calls */
+    connect( this, SIGNAL(askGetVideo(WId*,int*,int*,unsigned*,unsigned *)),
+             this, SLOT(getVideoSlot(WId*,int*,int*,unsigned*,unsigned*)),
+             Qt::BlockingQueuedConnection );
     connect( this, SIGNAL(askReleaseVideo( void )),
-             this, SLOT(releaseVideoSlot( void )), Qt::BlockingQueuedConnection );
+             this, SLOT(releaseVideoSlot( void )),
+             Qt::BlockingQueuedConnection );
 
     if( videoWidget )
     {
         CONNECT( this, askVideoToResize( unsigned int, unsigned int ),
                  videoWidget, SetSizing( unsigned int, unsigned int ) );
-
-        connect( this, SIGNAL(askVideoToShow( unsigned int, unsigned int)),
-             videoWidget, SLOT(SetSizing(unsigned int, unsigned int )),
-             Qt::BlockingQueuedConnection );
     }
 
     CONNECT( this, askUpdate(), this, doComponentsUpdate() );
@@ -257,7 +265,7 @@ MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
     /* Final sizing and showing */
     setMinimumWidth( __MAX( controls->sizeHint().width(),
                             menuBar()->sizeHint().width() ) );
-    show();
+    setVisible( !b_shouldHide );
 
     /* And switch to minimal view if needed
        Must be called after the show() */
@@ -269,18 +277,12 @@ MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
     updateGeometry();
     resize( sizeHint() );
 
-    /*****************************************************
-     * End everything by creating the Systray Management *
-     *****************************************************/
-    initSystray();
 }
 
 MainInterface::~MainInterface()
 {
     msg_Dbg( p_intf, "Destroying the main interface" );
 
-    delete dialogHandler;
-
     /* Unsure we hide the videoWidget before destroying it */
     if( videoIsActive ) videoWidget->hide();
 
@@ -371,7 +373,6 @@ void MainInterface::createMainWidget( QSettings *settings )
     bgWidget->resize(
             settings->value( "backgroundSize", QSize( 300, 200 ) ).toSize() );
     bgWidget->updateGeometry();
-    CONNECT( this, askBgWidgetToToggle(), bgWidget, toggle() );
 
     if( i_visualmode != QT_ALWAYS_VIDEO_MODE &&
         i_visualmode != QT_MINIMAL_MODE )
@@ -410,7 +411,7 @@ void MainInterface::createMainWidget( QSettings *settings )
     /* Create the FULLSCREEN CONTROLS Widget */
     if( config_GetInt( p_intf, "qt-fs-controller" ) )
     {
-        fullscreenControls = new FullscreenControllerWidget( p_intf );
+        fullscreenControls = new FullscreenControllerWidget( p_intf, this );
     }
 }
 
@@ -458,7 +459,7 @@ inline void MainInterface::initSystray()
         if( b_systrayAvailable )
         {
             b_systrayWanted = true;
-            hide();
+            b_shouldHide = true;
         }
         else
             msg_Err( p_intf, "cannot start minimized without system tray bar" );
@@ -633,7 +634,7 @@ QSize MainInterface::sizeHint() const
  */
 void MainInterface::doComponentsUpdate()
 {
-    if( isFullScreen() ) return;
+    if( isFullScreen() || isMaximized() ) return;
 
     msg_Dbg( p_intf, "Updating the geometry" );
     /* Here we resize to sizeHint() and not adjustsize because we want
@@ -658,6 +659,12 @@ void MainInterface::debug()
 #endif
 }
 
+void MainInterface::destroyPopupMenu()
+{
+    QVLCMenu::PopupMenu(p_intf, false );
+}
+
+
 void MainInterface::toggleFSC()
 {
    if( !fullscreenControls ) return;
@@ -695,40 +702,54 @@ private:
 };
 
 /**
- * README
- * Thou shall not call/resize/hide widgets from on another thread.
- * This is wrong, and this is THE reason to emit signals on those Video Functions
- **/
-WId MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
-                                 int *pi_y, unsigned int *pi_width,
-                                 unsigned int *pi_height )
+ * NOTE:
+ * You must note change the state of this object or other Qt4 UI objects,
+ * from the video output thread - only from the Qt4 UI main loop thread.
+ * All window provider queries must be handled through signals or events.
+ * That's why we have all those emit statements...
+ */
+WId MainInterface::getVideo( int *pi_x, int *pi_y,
+                             unsigned int *pi_width, unsigned int *pi_height )
+{
+    if( !videoWidget )
+        return 0;
+
+    /* This is a blocking call signal. Results are returned through pointers.
+     * Beware of deadlocks! */
+    WId id;
+    emit askGetVideo( &id, pi_x, pi_y, pi_width, pi_height );
+    return id;
+}
+
+void MainInterface::getVideoSlot( WId *p_id, int *pi_x, int *pi_y,
+                                  unsigned *pi_width, unsigned *pi_height )
 {
     /* Request the videoWidget */
-    WId ret = videoWidget->request( p_nvout,pi_x, pi_y,
+    WId ret = videoWidget->request( pi_x, pi_y,
                                     pi_width, pi_height, b_keep_size );
+    *p_id = ret;
     if( ret ) /* The videoWidget is available */
     {
         /* Did we have a bg ? Hide it! */
         if( VISIBLE( bgWidget) )
         {
             bgWasVisible = true;
-            emit askBgWidgetToToggle();
+            bgWidget->toggle();
         }
         else
             bgWasVisible = false;
 
         /* ask videoWidget to show */
-        emit askVideoToShow( *pi_width, *pi_height );
+        videoWidget->SetSizing( *pi_width, *pi_height );
 
         /* Consider the video active now */
         videoIsActive = true;
 
         emit askUpdate();
     }
-    return ret;
 }
 
-/* Call from the WindowClose function */
+/* Asynchronous call from the WindowClose function */
 void MainInterface::releaseVideo( void )
 {
     emit askReleaseVideo( );
@@ -749,35 +770,32 @@ void MainInterface::releaseVideoSlot( void )
     videoIsActive = false;
 
     /* Try to resize, except when you are in Fullscreen mode */
-    if( !isFullScreen() ) doComponentsUpdate();
+    doComponentsUpdate();
 }
 
-/* Call from WindowControl function */
+/* Asynchronous call from WindowControl function */
 int MainInterface::controlVideo( int i_query, va_list args )
 {
-    int i_ret = VLC_SUCCESS;
     switch( i_query )
     {
-        case VOUT_SET_SIZE:
-        {
-            unsigned int i_width  = va_arg( args, unsigned int );
-            unsigned int i_height = va_arg( args, unsigned int );
-            emit askVideoToResize( i_width, i_height );
-            emit askUpdate();
-            break;
-        }
-        case VOUT_SET_STAY_ON_TOP:
-        {
-            int i_arg = va_arg( args, int );
-            QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
-            break;
-        }
-        default:
-            i_ret = VLC_EGENERIC;
-            msg_Warn( p_intf, "unsupported control query" );
-            break;
+    case VOUT_WINDOW_SET_SIZE:
+    {
+        unsigned int i_width  = va_arg( args, unsigned int );
+        unsigned int i_height = va_arg( args, unsigned int );
+        emit askVideoToResize( i_width, i_height );
+        emit askUpdate();
+        return VLC_SUCCESS;
+    }
+    case VOUT_WINDOW_SET_ON_TOP:
+    {
+        int i_arg = va_arg( args, int );
+        QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
+        return VLC_SUCCESS;
+    }
+    default:
+        msg_Warn( p_intf, "unsupported control query" );
+        return VLC_EGENERIC;
     }
-    return i_ret;
 }
 
 /*****************************************************************************
@@ -842,7 +860,7 @@ void MainInterface::toggleMinimalView( bool b_switch )
     { /* NORMAL MODE then */
         if( !videoWidget || videoWidget->isHidden() )
         {
-            emit askBgWidgetToToggle();
+            bgWidget->toggle();
         }
         else
         {
@@ -902,7 +920,7 @@ void MainInterface::visual()
 /************************************************************************
  * Other stuff
  ************************************************************************/
-void MainInterface::setName( QString name )
+void MainInterface::setName( const QString& name )
 {
     input_name = name; /* store it for the QSystray use */
     /* Display it in the status bar, but also as a Tooltip in case it doesn't
@@ -915,7 +933,7 @@ void MainInterface::setName( QString name )
  * Give the decorations of the Main Window a correct Name.
  * If nothing is given, set it to VLC...
  **/
-void MainInterface::setVLCWindowsTitle( QString aTitle )
+void MainInterface::setVLCWindowsTitle( const QString& aTitle )
 {
     if( aTitle.isEmpty() )
     {
@@ -953,9 +971,9 @@ void MainInterface::createSystray()
 {
     QIcon iconVLC;
     if( QDate::currentDate().dayOfYear() >= 354 )
-        iconVLC =  QIcon( QPixmap( ":/vlc128-christmas.png" ) );
+        iconVLC =  QIcon( QPixmap( ":/logo/vlc128-christmas.png" ) );
     else
-        iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
+        iconVLC =  QIcon( QPixmap( ":/logo/vlc128.png" ) );
     sysTray = new QSystemTrayIcon( iconVLC, this );
     sysTray->setToolTip( qtr( "VLC media player" ));
 
@@ -1040,7 +1058,7 @@ void MainInterface::handleSystrayClick(
  * Updates the name of the systray Icon tooltip.
  * Doesn't check if the systray exists, check before you call it.
  **/
-void MainInterface::updateSystrayTooltipName( QString name )
+void MainInterface::updateSystrayTooltipName( const QString& name )
 {
     if( name.isEmpty() )
     {
@@ -1085,6 +1103,7 @@ void MainInterface::updateSystrayTooltipStatus( int i_status )
                 break;
             }
     }
+    QVLCMenu::updateSystrayMenu( this, p_intf );
 }
 
 /************************************************************************
@@ -1125,8 +1144,8 @@ void MainInterface::dropEventPlay( QDropEvent *event, bool b_play )
 
         if( s.length() > 0 ) {
             playlist_Add( THEPL, qtu(s), NULL,
-                          PLAYLIST_APPEND | (first ? PLAYLIST_GO: 0),
-                          PLAYLIST_END, true, false );
+                          PLAYLIST_APPEND | (first ? PLAYLIST_GO: PLAYLIST_PREPARSE),
+                          PLAYLIST_END, true, pl_Unlocked );
             first = false;
             RecentsMRL::getInstance( p_intf )->addRecent( s );
         }
@@ -1166,10 +1185,10 @@ void MainInterface::customEvent( QEvent *event )
     {
         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
         if( p_event->OnTop() )
-            setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
+            setWindowFlags( windowFlags() | Qt::WindowStaysOnTopHint );
         else
-            setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
-        show(); /* necessary to apply window flags?? */
+            setWindowFlags( windowFlags() & ~Qt::WindowStaysOnTopHint );
+        show(); /* necessary to apply window flags */
     }
 }