]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
3ac08fcabbe6e8ae131d989f1690473e099ca577
[vlc] / modules / gui / qt4 / main_interface.cpp
1 /*****************************************************************************
2  * main_interface.cpp : Main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *          Ilkka Ollakka <ileoo@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "qt4.hpp"
31 #include "main_interface.hpp"
32 #include "input_manager.hpp"
33 #include "util/qvlcframe.hpp"
34 #include "util/customwidgets.hpp"
35 #include "dialogs_provider.hpp"
36 #include "components/interface_widgets.hpp"
37 #include "components/controller.hpp"
38 #include "components/playlist/playlist.hpp"
39 #include "dialogs/extended.hpp"
40 #include "dialogs/playlist.hpp"
41 #include "menus.hpp"
42
43 #include <QMenuBar>
44 #include <QCloseEvent>
45 #include <QPushButton>
46 #include <QStatusBar>
47 #include <QKeyEvent>
48 #include <QUrl>
49 #include <QSystemTrayIcon>
50 #include <QSize>
51 #include <QMenu>
52 #include <QLabel>
53 #include <QSlider>
54 #include <QWidgetAction>
55 #include <QToolBar>
56 #include <QGroupBox>
57 #include <QDate>
58
59 #include <assert.h>
60 #include <vlc_keys.h>
61 #include <vlc_vout.h>
62
63 /* Callback prototypes */
64 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
65                         vlc_value_t old_val, vlc_value_t new_val, void *param );
66 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
67                        vlc_value_t old_val, vlc_value_t new_val, void *param );
68 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
69                              vlc_value_t, void *);
70
71 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
72 {
73     /* Variables initialisation */
74     // need_components_update = false;
75     bgWidget             = NULL;
76     videoWidget          = NULL;
77     playlistWidget       = NULL;
78     sysTray              = NULL;
79     videoIsActive        = false;
80     playlistVisible      = false;
81     input_name           = "";
82     fullscreenControls   = NULL;
83
84     /* Ask for privacy */
85     askForPrivacy();
86
87     /**
88      *  Configuration and settings
89      *  Pre-building of interface
90      **/
91     /* Main settings */
92     setFocusPolicy( Qt::StrongFocus );
93     setAcceptDrops( true );
94     setWindowIcon( QApplication::windowIcon() );
95     setWindowOpacity( config_GetFloat( p_intf, "qt-opacity" ) );
96
97     /* Set The Video In emebedded Mode or not */
98     videoEmbeddedFlag = config_GetInt( p_intf, "embedded-video" );
99
100     /* Are we in the enhanced always-video mode or not ? */
101     i_visualmode = config_GetInt( p_intf, "qt-display-mode" );
102
103     /* Set the other interface settings */
104     settings = getSettings();
105     settings->beginGroup( "MainWindow" );
106
107     /* Visualisation, not really used yet */
108     visualSelectorEnabled = settings->value( "visual-selector", false).toBool();
109
110     /* Do we want anoying popups or not */
111     notificationEnabled = (bool)config_GetInt( p_intf, "qt-notification" );
112
113     /**************************
114      *  UI and Widgets design
115      **************************/
116     setVLCWindowsTitle();
117     handleMainUi( settings );
118
119 #if 0
120     /* Create a Dock to get the playlist */
121     dockPL = new QDockWidget( qtr( "Playlist" ), this );
122     dockPL->setSizePolicy( QSizePolicy::Preferred,
123                            QSizePolicy::Expanding );
124     dockPL->setFeatures( QDockWidget::AllDockWidgetFeatures );
125     dockPL->setAllowedAreas( Qt::LeftDockWidgetArea
126                            | Qt::RightDockWidgetArea
127                            | Qt::BottomDockWidgetArea );
128     dockPL->hide();
129 #endif
130
131     /**************************
132      * Menu Bar and Status Bar
133      **************************/
134     QVLCMenu::createMenuBar( this, p_intf, visualSelectorEnabled );
135     /* StatusBar Creation */
136     createStatusBar();
137
138
139     /********************
140      * Input Manager    *
141      ********************/
142     MainInputManager::getInstance( p_intf );
143
144     /**************************
145      * Various CONNECTs on IM *
146      **************************/
147     /* Connect the input manager to the GUI elements it manages */
148
149     /* It is also connected to the control->slider, see the ControlsWidget */
150     /* Change the SpeedRate in the Status */
151     CONNECT( THEMIM->getIM(), rateChanged( int ), this, setRate( int ) );
152
153     /**
154      * Connects on nameChanged()
155      * Those connects are not merged because different options can trigger
156      * them down.
157      */
158     /* Naming in the controller statusbar */
159     CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
160              setName( QString ) );
161     /* and in the systray */
162     if( sysTray )
163     {
164         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
165                  updateSystrayTooltipName( QString ) );
166     }
167     /* and in the title of the controller */
168     if( config_GetInt( p_intf, "qt-name-in-title" ) )
169     {
170         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
171              setVLCWindowsTitle( QString ) );
172     }
173
174     /**
175      * CONNECTS on PLAY_STATUS
176      **/
177     /* Status on the main controller */
178     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
179     /* and in the systray */
180     if( sysTray )
181     {
182         CONNECT( THEMIM->getIM(), statusChanged( int ), this,
183                  updateSystrayTooltipStatus( int ) );
184     }
185
186     /* END CONNECTS ON IM */
187
188
189     /************
190      * Callbacks
191      ************/
192     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
193     var_AddCallback( p_intf, "interaction", InteractCallback, this );
194     p_intf->b_interaction = true;
195
196     var_AddCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
197
198     /* Register callback for the intf-popupmenu variable */
199     var_AddCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
200
201
202     /* VideoWidget connects to avoid different threads speaking to each other */
203     CONNECT( this, askReleaseVideo( void * ),
204              this, releaseVideoSlot( void * ) );
205     if( videoWidget )
206         CONNECT( this, askVideoToResize( unsigned int, unsigned int ),
207                  videoWidget, SetSizing( unsigned int, unsigned int ) );
208
209     CONNECT( this, askUpdate(), this, doComponentsUpdate() );
210
211     /* Size and placement of interface */
212     settings->beginGroup( "MainWindow" );
213     QVLCTools::restoreWidgetPosition( settings, this, QSize(380, 60) );
214
215     bool b_visible = settings->value( "playlist-visible", 0 ).toInt();
216     settings->endGroup();
217
218     /* Playlist */
219     if( b_visible ) togglePlaylist();
220
221     /* Final sizing and showing */
222     setMinimumWidth( __MAX( controls->sizeHint().width(),
223                             menuBar()->sizeHint().width() ) );
224     show();
225
226     /* And switch to minimal view if needed
227        Must be called after the show() */
228     if( i_visualmode == QT_MINIMAL_MODE )
229         toggleMinimalView();
230
231     /* Update the geometry : It is useful if you switch between
232        qt-display-modes ?*/
233     updateGeometry();
234     resize( sizeHint() );
235
236     /*****************************************************
237      * End everything by creating the Systray Management *
238      *****************************************************/
239     initSystray();
240 }
241
242 MainInterface::~MainInterface()
243 {
244     msg_Dbg( p_intf, "Destroying the main interface" );
245
246     if( videoIsActive ) videoWidget->hide();
247
248     if( playlistWidget )
249     {
250         if( !isDocked() )
251             QVLCTools::saveWidgetPosition( p_intf, "Playlist", playlistWidget );
252
253         delete playlistWidget;
254     }
255
256     settings->beginGroup( "MainWindow" );
257
258     settings->setValue( "pl-dock-status", (int)i_pl_dock );
259     settings->setValue( "playlist-visible", (int)playlistVisible );
260     settings->setValue( "adv-controls",
261                         getControlsVisibilityStatus() & CONTROLS_ADVANCED );
262
263     if( bgWidget )
264         settings->setValue( "backgroundSize", bgWidget->size() );
265
266     QVLCTools::saveWidgetPosition(settings, this);
267     settings->endGroup();
268
269     var_DelCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
270
271     /* Unregister callback for the intf-popupmenu variable */
272     var_DelCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
273
274     p_intf->b_interaction = false;
275     var_DelCallback( p_intf, "interaction", InteractCallback, this );
276
277     p_intf->p_sys->p_mi = NULL;
278 }
279
280 /*****************************
281  *   Main UI handling        *
282  *****************************/
283
284 inline void MainInterface::createStatusBar()
285 {
286     /****************
287      *  Status Bar  *
288      ****************/
289     /* Widgets Creation*/
290     timeLabel = new TimeLabel( p_intf );
291     nameLabel = new QLabel;
292     nameLabel->setTextInteractionFlags( Qt::TextSelectableByMouse
293                                       | Qt::TextSelectableByKeyboard );
294     speedLabel = new SpeedLabel( p_intf, "1.00x" );
295     speedLabel->setToolTip(
296             qtr( "Current playback speed.\nRight click to adjust" ) );
297     speedLabel->setContextMenuPolicy ( Qt::CustomContextMenu );
298
299     /* Styling those labels */
300     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
301     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
302     nameLabel->setFrameStyle( QFrame::Sunken | QFrame::StyledPanel);
303
304     /* and adding those */
305     statusBar()->addWidget( nameLabel, 8 );
306     statusBar()->addPermanentWidget( speedLabel, 0 );
307     statusBar()->addPermanentWidget( timeLabel, 0 );
308
309     /* timeLabel behaviour:
310        - double clicking opens the goto time dialog
311        - right-clicking and clicking just toggle between remaining and
312          elapsed time.*/
313     CONNECT( timeLabel, timeLabelDoubleClicked(), THEDP, gotoTimeDialog() );
314
315     /* Speed Label behaviour:
316        - right click gives the vertical speed slider */
317     CONNECT( speedLabel, customContextMenuRequested( QPoint ),
318              this, showSpeedMenu( QPoint ) );
319 }
320
321 inline void MainInterface::initSystray()
322 {
323     bool b_createSystray = false;
324     bool b_systrayAvailable = QSystemTrayIcon::isSystemTrayAvailable();
325     if( config_GetInt( p_intf, "qt-start-minimized") )
326     {
327         if( b_systrayAvailable )
328         {
329             b_createSystray = true;
330             hide();
331         }
332         else msg_Err( p_intf, "You can't minimize if you haven't a system "
333                 "tray bar" );
334     }
335     if( config_GetInt( p_intf, "qt-system-tray") )
336         b_createSystray = true;
337
338     if( b_systrayAvailable && b_createSystray )
339             createSystray();
340 }
341
342 /**
343  * Give the decorations of the Main Window a correct Name.
344  * If nothing is given, set it to VLC...
345  **/
346 void MainInterface::setVLCWindowsTitle( QString aTitle )
347 {
348     if( aTitle.isEmpty() )
349     {
350         setWindowTitle( qtr( "VLC media player" ) );
351     }
352     else
353     {
354         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
355     }
356 }
357
358 void MainInterface::handleMainUi( QSettings *settings )
359 {
360     /* Create the main Widget and the mainLayout */
361     QWidget *main = new QWidget;
362     setCentralWidget( main );
363     mainLayout = new QVBoxLayout( main );
364
365     /* Margins, spacing */
366     main->setContentsMargins( 0, 0, 0, 0 );
367     main->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
368     mainLayout->setSpacing( 0 );
369     mainLayout->setMargin( 0 );
370
371     /* Create the CONTROLS Widget */
372     controls = new ControlsWidget( p_intf,
373                    settings->value( "adv-controls", false ).toBool() );
374     CONNECT( controls, advancedControlsToggled( bool ),
375              this, doComponentsUpdate() );
376
377     /* Add the controls Widget to the main Widget */
378     mainLayout->insertWidget( 0, controls, 0, Qt::AlignBottom );
379
380     /* Create the Speed Control Widget */
381     speedControl = new SpeedControlWidget( p_intf );
382     speedControlMenu = new QMenu( this );
383
384     QWidgetAction *widgetAction = new QWidgetAction( speedControl );
385     widgetAction->setDefaultWidget( speedControl );
386     speedControlMenu->addAction( widgetAction );
387
388     /* Visualisation */
389     /* Disabled for now, they SUCK */
390     #if 0
391     visualSelector = new VisualSelector( p_intf );
392     mainLayout->insertWidget( 0, visualSelector );
393     visualSelector->hide();
394     #endif
395
396     /* Bg Cone */
397     bgWidget = new BackgroundWidget( p_intf );
398     bgWidget->resize(
399             settings->value( "backgroundSize", QSize( 300, 200 ) ).toSize() );
400     bgWidget->updateGeometry();
401     mainLayout->insertWidget( 0, bgWidget );
402     CONNECT( this, askBgWidgetToToggle(), bgWidget, toggle() );
403
404     if( i_visualmode != QT_ALWAYS_VIDEO_MODE &&
405         i_visualmode != QT_MINIMAL_MODE )
406     {
407         bgWidget->hide();
408     }
409
410     /* And video Outputs */
411     if( videoEmbeddedFlag )
412     {
413         videoWidget = new VideoWidget( p_intf );
414         mainLayout->insertWidget( 0, videoWidget, 10 );
415     }
416
417     /* Finish the sizing */
418     main->updateGeometry();
419
420     getSettings()->endGroup();
421 #ifdef WIN32
422     if ( depth() > 8 )
423 #endif
424     /* Create the FULLSCREEN CONTROLS Widget */
425     if( config_GetInt( p_intf, "qt-fs-controller" ) )
426     {
427         fullscreenControls = new FullscreenControllerWidget( p_intf );
428     }
429 }
430
431 inline void MainInterface::askForPrivacy()
432 {
433     /**
434      * Ask for the network policy on FIRST STARTUP
435      **/
436     if( config_GetInt( p_intf, "qt-privacy-ask") )
437     {
438         QList<ConfigControl *> controls;
439         if( privacyDialog( &controls ) == QDialog::Accepted )
440         {
441             QList<ConfigControl *>::Iterator i;
442             for(  i = controls.begin() ; i != controls.end() ; i++ )
443             {
444                 ConfigControl *c = qobject_cast<ConfigControl *>(*i);
445                 c->doApply( p_intf );
446             }
447
448             config_PutInt( p_intf,  "qt-privacy-ask" , 0 );
449             /* We have to save here because the user may not launch Prefs */
450             config_SaveConfigFile( p_intf, NULL );
451         }
452     }
453 }
454
455 int MainInterface::privacyDialog( QList<ConfigControl *> *controls )
456 {
457     QDialog *privacy = new QDialog();
458
459     privacy->setWindowTitle( qtr( "Privacy and Network Policies" ) );
460
461     QGridLayout *gLayout = new QGridLayout( privacy );
462
463     QGroupBox *blabla = new QGroupBox( qtr( "Privacy and Network Warning" ) );
464     QGridLayout *blablaLayout = new QGridLayout( blabla );
465     QLabel *text = new QLabel( qtr(
466         "<p>The <i>VideoLAN Team</i> doesn't like when an application goes "
467         "online without authorization.</p>\n "
468         "<p><i>VLC media player</i> can retreive limited information from "
469         "the Internet in order to get CD covers or to check "
470         "for available updates.</p>\n"
471         "<p><i>VLC media player</i> <b>DOES NOT</b> send or collect <b>ANY</b> "
472         "information, even anonymously, about your usage.</p>\n"
473         "<p>Therefore please select from the following options, the default being "
474         "almost no access to the web.</p>\n") );
475     text->setWordWrap( true );
476     text->setTextFormat( Qt::RichText );
477
478     blablaLayout->addWidget( text, 0, 0 ) ;
479
480     QGroupBox *options = new QGroupBox;
481     QGridLayout *optionsLayout = new QGridLayout( options );
482
483     gLayout->addWidget( blabla, 0, 0, 1, 3 );
484     gLayout->addWidget( options, 1, 0, 1, 3 );
485     module_config_t *p_config;
486     ConfigControl *control;
487     int line = 0;
488
489 #define CONFIG_GENERIC( option, type )                            \
490     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
491     if( p_config )                                                \
492     {                                                             \
493         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
494                 p_config, options, false, optionsLayout, line );  \
495         controls->append( control );                               \
496     }
497
498 #define CONFIG_GENERIC_NOBOOL( option, type )                     \
499     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
500     if( p_config )                                                \
501     {                                                             \
502         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
503                 p_config, options, optionsLayout, line );  \
504         controls->append( control );                               \
505     }
506
507     CONFIG_GENERIC( "album-art", IntegerList ); line++;
508 #ifdef UPDATE_CHECK
509     CONFIG_GENERIC_NOBOOL( "qt-updates-notif", Bool ); line++;
510     CONFIG_GENERIC_NOBOOL( "qt-updates-days", Integer ); line++;
511 #endif
512
513     QPushButton *ok = new QPushButton( qtr( "OK" ) );
514
515     gLayout->addWidget( ok, 2, 2 );
516
517     CONNECT( ok, clicked(), privacy, accept() );
518     return privacy->exec();
519 }
520
521
522 /**********************************************************************
523  * Handling of sizing of the components
524  **********************************************************************/
525
526 /* This function is probably wrong, but we don't have many many choices...
527    Since we can't know from the playlist Widget if we are inside a dock or not,
528    because the playlist Widget can be called by THEDP, as a separate windows for
529    the skins.
530    Maybe the other solution is to redefine the sizeHint() of the playlist and
531    ask _parent->isFloating()...
532    If you think this would be better, please FIXME it...
533 */
534
535 QSize MainInterface::sizeHint() const
536 {
537     int nwidth  = controls->sizeHint().width();
538     int nheight = controls->isVisible() ?
539                   controls->size().height()
540                   + menuBar()->size().height()
541                   + statusBar()->size().height()
542                   : 0 ;
543
544     if( VISIBLE( bgWidget ) )
545     {
546         nheight += bgWidget->size().height();
547         nwidth  = bgWidget->size().width();
548     }
549     else if( videoIsActive && videoWidget->isVisible() )
550     {
551         nheight += videoWidget->sizeHint().height();
552         nwidth  = videoWidget->sizeHint().width();
553     }
554 #if 0
555     if( !dockPL->isFloating() && dockPL->isVisible() && dockPL->widget()  )
556     {
557         nheight += dockPL->size().height();
558         nwidth = __MAX( nwidth, dockPL->size().width() );
559         msg_Warn( p_intf, "3 %i %i", nheight, nwidth );
560     }
561 #endif
562     return QSize( nwidth, nheight );
563 }
564
565 void MainInterface::toggleFSC()
566 {
567    if( !fullscreenControls ) return;
568
569    IMEvent *eShow = new IMEvent( FullscreenControlToggle_Type, 0 );
570    QApplication::postEvent( fullscreenControls, static_cast<QEvent *>(eShow) );
571 }
572
573 void MainInterface::debug()
574 {
575 #ifndef NDEBUG
576     msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
577     msg_Dbg( p_intf, "sizeHint: %i - %i", sizeHint().height(), sizeHint().width() );
578     if( videoWidget && videoWidget->isVisible() )
579     {
580         msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
581         msg_Dbg( p_intf, "sizeHint: %i - %i", sizeHint().height(), sizeHint().width() );
582     }
583 #endif
584 }
585
586 /****************************************************************************
587  * Small right-click menu for rate control
588  ****************************************************************************/
589 void MainInterface::showSpeedMenu( QPoint pos )
590 {
591     speedControlMenu->exec( QCursor::pos() - pos
592                           + QPoint( 0, speedLabel->height() ) );
593 }
594
595 /****************************************************************************
596  * Video Handling
597  ****************************************************************************/
598
599 /* This event is used to deal with the fullscreen and always on top
600    issue conflict (bug in wx) */
601 class SetVideoOnTopQtEvent : public QEvent
602 {
603 public:
604     SetVideoOnTopQtEvent( bool _onTop ) :
605       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
606     {}
607
608     bool OnTop() const { return onTop; }
609
610 private:
611     bool onTop;
612 };
613
614 /**
615  * README
616  * Thou shall not call/resize/hide widgets from on another thread.
617  * This is wrong, and this is TEH reason to emit signals on those Video Functions
618  **/
619 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
620                                    int *pi_y, unsigned int *pi_width,
621                                    unsigned int *pi_height )
622 {
623     /* Request the videoWidget */
624     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
625     if( ret ) /* The videoWidget is available */
626     {
627         /* Did we have a bg ? Hide it! */
628         if( VISIBLE( bgWidget) )
629         {
630             bgWasVisible = true;
631             emit askBgWidgetToToggle();
632         }
633         else
634             bgWasVisible = false;
635
636         /* Consider the video active now */
637         videoIsActive = true;
638
639         emit askUpdate();
640
641         if( fullscreenControls ) fullscreenControls->attachVout( p_nvout );
642     }
643     return ret;
644 }
645
646 /* Call from the WindowClose function */
647 void MainInterface::releaseVideo( void *p_win )
648 {
649     if( fullscreenControls ) fullscreenControls->detachVout();
650     if( p_win )
651         emit askReleaseVideo( p_win );
652 }
653
654 /* Function that is CONNECTED to the previous emit */
655 void MainInterface::releaseVideoSlot( void *p_win )
656 {
657     videoWidget->release( p_win );
658
659     if( bgWasVisible )
660     {
661         /* Reset the bg state */
662         bgWasVisible = false;
663         bgWidget->show();
664     }
665
666     videoIsActive = false;
667
668     /* Try to resize, except when you are in Fullscreen mode */
669     if( !isFullScreen() ) doComponentsUpdate();
670 }
671
672 /* Call from WindowControl function */
673 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
674 {
675     int i_ret = VLC_SUCCESS;
676     switch( i_query )
677     {
678         case VOUT_GET_SIZE:
679         {
680             unsigned int *pi_width  = va_arg( args, unsigned int * );
681             unsigned int *pi_height = va_arg( args, unsigned int * );
682             *pi_width = videoWidget->videoSize.width();
683             *pi_height = videoWidget->videoSize.height();
684             break;
685         }
686         case VOUT_SET_SIZE:
687         {
688             unsigned int i_width  = va_arg( args, unsigned int );
689             unsigned int i_height = va_arg( args, unsigned int );
690             emit askVideoToResize( i_width, i_height );
691             emit askUpdate();
692             break;
693         }
694         case VOUT_SET_STAY_ON_TOP:
695         {
696             int i_arg = va_arg( args, int );
697             QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
698             break;
699         }
700         default:
701             i_ret = VLC_EGENERIC;
702             msg_Warn( p_intf, "unsupported control query" );
703             break;
704     }
705     return i_ret;
706 }
707
708 /*****************************************************************************
709  * Playlist, Visualisation and Menus handling
710  *****************************************************************************/
711 /**
712  * Toggle the playlist widget or dialog
713  **/
714 void MainInterface::togglePlaylist()
715 {
716     /* CREATION
717     If no playlist exist, then create one and attach it to the DockPL*/
718     if( !playlistWidget )
719     {
720         playlistWidget = new PlaylistWidget( p_intf );
721
722         i_pl_dock = PL_UNDOCKED;
723 /*        i_pl_dock = (pl_dock_e)getSettings()
724                          ->value( "pl-dock-status", PL_UNDOCKED ).toInt(); */
725
726         if( i_pl_dock == PL_UNDOCKED )
727         {
728             playlistWidget->setWindowFlags( Qt::Window );
729
730             /* This will restore the geometry but will not work for position,
731                because of parenting */
732             QVLCTools::restoreWidgetPosition( p_intf, "Playlist",
733                     playlistWidget, QSize( 600, 300 ) );
734         }
735         else
736         {
737             mainLayout->insertWidget( 4, playlistWidget );
738         }
739         playlistVisible = true;
740
741         playlistWidget->show();
742     }
743     else
744     {
745     /* toggle the visibility of the playlist */
746        TOGGLEV( playlistWidget );
747        playlistVisible = !playlistVisible;
748        //doComponentsUpdate(); //resize( sizeHint() );
749     }
750 }
751
752 /* Function called from the menu to undock the playlist */
753 void MainInterface::undockPlaylist()
754 {
755 //    dockPL->setFloating( true );
756 //    adjustSize();
757 }
758
759 void MainInterface::dockPlaylist( pl_dock_e i_pos )
760 {
761 }
762
763 void MainInterface::toggleMinimalView()
764 {
765     /* HACK for minimalView, see menus.cpp */
766     if( !menuBar()->isVisible() ) QVLCMenu::minimalViewAction->toggle();
767
768     if( i_visualmode != QT_ALWAYS_VIDEO_MODE &&
769         i_visualmode != QT_MINIMAL_MODE )
770     { /* NORMAL MODE then */
771         if( videoWidget->isHidden() ) emit askBgWidgetToToggle();
772         else
773         {
774             /* If video is visible, then toggle the status of bgWidget */
775             bgWasVisible = !bgWasVisible;
776         }
777     }
778
779     TOGGLEV( menuBar() );
780     TOGGLEV( controls );
781     TOGGLEV( statusBar() );
782     doComponentsUpdate();
783 }
784
785 /* Video widget cannot do this synchronously as it runs in another thread */
786 /* Well, could it, actually ? Probably dangerous ... */
787
788 /* This function is called:
789    - toggling of minimal View
790    - through askUpdate() by Vout thread request video and resize video (zoom)
791    - Advanced buttons toggled
792  */
793 void MainInterface::doComponentsUpdate()
794 {
795     msg_Dbg( p_intf, "Updating the geometry" );
796     /* Here we resize to sizeHint() and not adjustsize because we want
797        the videoWidget to be exactly the correctSize */
798     resize( sizeHint() );
799     //    adjustSize()  ;
800 #ifndef NDEBUG
801     debug();
802 #endif
803 }
804
805 /* toggling advanced controls buttons */
806 void MainInterface::toggleAdvanced()
807 {
808     controls->toggleAdvanced();
809 //    if( fullscreenControls ) fullscreenControls->toggleAdvanced();
810 }
811
812 /* Get the visibility status of the controls (hidden or not, advanced or not) */
813 int MainInterface::getControlsVisibilityStatus()
814 {
815     return( (controls->isVisible() ? CONTROLS_VISIBLE : CONTROLS_HIDDEN )
816                 + CONTROLS_ADVANCED * controls->b_advancedVisible );
817 }
818
819 #if 0
820 void MainInterface::visual()
821 {
822     if( !VISIBLE( visualSelector) )
823     {
824         visualSelector->show();
825         if( !THEMIM->getIM()->hasVideo() )
826         {
827             /* Show the background widget */
828         }
829         visualSelectorEnabled = true;
830     }
831     else
832     {
833         /* Stop any currently running visualization */
834         visualSelector->hide();
835         visualSelectorEnabled = false;
836     }
837     doComponentsUpdate();
838 }
839 #endif
840
841 /************************************************************************
842  * Other stuff
843  ************************************************************************/
844 void MainInterface::setName( QString name )
845 {
846     input_name = name; /* store it for the QSystray use */
847     /* Display it in the status bar, but also as a Tooltip in case it doesn't
848        fit in the label */
849     nameLabel->setText( " " + name + " " );
850     nameLabel->setToolTip( " " + name +" " );
851 }
852
853 void MainInterface::setStatus( int status )
854 {
855     msg_Dbg( p_intf, "Updating the stream status: %i", status );
856
857     speedControl->setEnable( THEMIM->getIM()->hasInput() );
858
859     /* And in the systray for the menu */
860     if( sysTray )
861         QVLCMenu::updateSystrayMenu( this, p_intf );
862 }
863
864 void MainInterface::setRate( int rate )
865 {
866     QString str;
867     str.setNum( ( 1000 / (double)rate ), 'f', 2 );
868     str.append( "x" );
869     speedLabel->setText( str );
870     speedLabel->setToolTip( str );
871     speedControl->updateControls( rate );
872 }
873
874 /*****************************************************************************
875  * Systray Icon and Systray Menu
876  *****************************************************************************/
877
878 /**
879  * Create a SystemTray icon and a menu that would go with it.
880  * Connects to a click handler on the icon.
881  **/
882 void MainInterface::createSystray()
883 {
884     QIcon iconVLC;
885     if( QDate::currentDate().dayOfYear() >= 354 )
886         iconVLC =  QIcon( QPixmap( ":/vlc128-christmas.png" ) );
887     else
888         iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
889     sysTray = new QSystemTrayIcon( iconVLC, this );
890     sysTray->setToolTip( qtr( "VLC media player" ));
891
892     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
893     systrayMenu->setIcon( iconVLC );
894
895     QVLCMenu::updateSystrayMenu( this, p_intf, true );
896     sysTray->show();
897
898     CONNECT( sysTray, activated( QSystemTrayIcon::ActivationReason ),
899             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
900 }
901
902 /**
903  * Updates the Systray Icon's menu and toggle the main interface
904  */
905 void MainInterface::toggleUpdateSystrayMenu()
906 {
907     /* If hidden, show it */
908     if( isHidden() )
909     {
910         show();
911         activateWindow();
912     }
913     else if( isMinimized() )
914     {
915         /* Minimized */
916         showNormal();
917         activateWindow();
918     }
919     else
920     {
921         /* Visible */
922 #ifdef WIN32
923         /* check if any visible window is above vlc in the z-order,
924          * but ignore the ones always on top */
925         WINDOWINFO wi;
926         HWND hwnd;
927         wi.cbSize = sizeof( WINDOWINFO );
928         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
929                 hwnd && !IsWindowVisible( hwnd );
930                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
931         if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
932                 (wi.dwExStyle&WS_EX_TOPMOST) )
933 #else
934         if( isActiveWindow() )
935 #endif
936         {
937             hide();
938         }
939         else
940         {
941             activateWindow();
942         }
943     }
944     QVLCMenu::updateSystrayMenu( this, p_intf );
945 }
946
947 void MainInterface::handleSystrayClick(
948                                     QSystemTrayIcon::ActivationReason reason )
949 {
950     switch( reason )
951     {
952         case QSystemTrayIcon::Trigger:
953             toggleUpdateSystrayMenu();
954             break;
955         case QSystemTrayIcon::MiddleClick:
956             sysTray->showMessage( qtr( "VLC media player" ),
957                     qtr( "Control menu for the player" ),
958                     QSystemTrayIcon::Information, 3000 );
959             break;
960     }
961 }
962
963 /**
964  * Updates the name of the systray Icon tooltip.
965  * Doesn't check if the systray exists, check before you call it.
966  **/
967 void MainInterface::updateSystrayTooltipName( QString name )
968 {
969     if( name.isEmpty() )
970     {
971         sysTray->setToolTip( qtr( "VLC media player" ) );
972     }
973     else
974     {
975         sysTray->setToolTip( name );
976         if( notificationEnabled && ( isHidden() || isMinimized() ) )
977         {
978             sysTray->showMessage( qtr( "VLC media player" ), name,
979                     QSystemTrayIcon::NoIcon, 3000 );
980         }
981     }
982 }
983
984 /**
985  * Updates the status of the systray Icon tooltip.
986  * Doesn't check if the systray exists, check before you call it.
987  **/
988 void MainInterface::updateSystrayTooltipStatus( int i_status )
989 {
990     switch( i_status )
991     {
992         case  0:
993         case  END_S:
994             {
995                 sysTray->setToolTip( qtr( "VLC media player" ) );
996                 break;
997             }
998         case PLAYING_S:
999             {
1000                 sysTray->setToolTip( input_name );
1001                 break;
1002             }
1003         case PAUSE_S:
1004             {
1005                 sysTray->setToolTip( input_name + " - "
1006                         + qtr( "Paused") );
1007                 break;
1008             }
1009     }
1010 }
1011
1012 /************************************************************************
1013  * D&D Events
1014  ************************************************************************/
1015 void MainInterface::dropEvent(QDropEvent *event)
1016 {
1017     dropEventPlay( event, true );
1018 }
1019
1020 void MainInterface::dropEventPlay( QDropEvent *event, bool b_play )
1021 {
1022      const QMimeData *mimeData = event->mimeData();
1023
1024      /* D&D of a subtitles file, add it on the fly */
1025      if( mimeData->urls().size() == 1 )
1026      {
1027         if( THEMIM->getIM()->hasInput() )
1028         {
1029             if( input_AddSubtitles( THEMIM->getInput(),
1030                                     qtu( toNativeSeparators(
1031                                          mimeData->urls()[0].toLocalFile() ) ),
1032                                     true ) )
1033             {
1034                 event->acceptProposedAction();
1035                 return;
1036             }
1037         }
1038      }
1039      bool first = b_play;
1040      foreach( QUrl url, mimeData->urls() )
1041      {
1042         QString s = toNativeSeparators( url.toLocalFile() );
1043
1044         if( s.length() > 0 ) {
1045             playlist_Add( THEPL, qtu(s), NULL,
1046                           PLAYLIST_APPEND | (first ? PLAYLIST_GO: 0),
1047                           PLAYLIST_END, true, false );
1048             first = false;
1049         }
1050      }
1051      event->acceptProposedAction();
1052 }
1053 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
1054 {
1055      event->acceptProposedAction();
1056 }
1057 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
1058 {
1059      event->acceptProposedAction();
1060 }
1061 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
1062 {
1063      event->accept();
1064 }
1065
1066 /************************************************************************
1067  * Events stuff
1068  ************************************************************************/
1069 void MainInterface::customEvent( QEvent *event )
1070 {
1071 #if 0
1072     if( event->type() == PLDockEvent_Type )
1073     {
1074         PlaylistDialog::killInstance();
1075         playlistEmbeddedFlag = true;
1076         menuBar()->clear();
1077         QVLCMenu::createMenuBar(this, p_intf, true, visualSelectorEnabled);
1078         togglePlaylist();
1079     }
1080 #endif
1081     /*else */
1082     if ( event->type() == SetVideoOnTopEvent_Type )
1083     {
1084         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
1085         if( p_event->OnTop() )
1086             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
1087         else
1088             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
1089         show(); /* necessary to apply window flags?? */
1090     }
1091 }
1092
1093 void MainInterface::keyPressEvent( QKeyEvent *e )
1094 {
1095     if( ( e->modifiers() &  Qt::ControlModifier ) && ( e->key() == Qt::Key_H )
1096           && menuBar()->isHidden() )
1097     {
1098         toggleMinimalView();
1099         e->accept();
1100     }
1101
1102     int i_vlck = qtEventToVLCKey( e );
1103     if( i_vlck > 0 )
1104     {
1105         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1106         e->accept();
1107     }
1108     else
1109         e->ignore();
1110 }
1111
1112 void MainInterface::wheelEvent( QWheelEvent *e )
1113 {
1114     int i_vlckey = qtWheelEventToVLCKey( e );
1115     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
1116     e->accept();
1117 }
1118
1119 void MainInterface::closeEvent( QCloseEvent *e )
1120 {
1121     hide();
1122     THEDP->quit();
1123 }
1124
1125 void MainInterface::toggleFullScreen( void )
1126 {
1127     if( isFullScreen() )
1128     {
1129         showNormal();
1130         emit askUpdate(); // Needed if video was launched after the F11
1131     }
1132     else
1133         showFullScreen();
1134 }
1135
1136 /*****************************************************************************
1137  * Callbacks
1138  *****************************************************************************/
1139 static int InteractCallback( vlc_object_t *p_this,
1140                              const char *psz_var, vlc_value_t old_val,
1141                              vlc_value_t new_val, void *param )
1142 {
1143     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
1144     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
1145     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
1146     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
1147     return VLC_SUCCESS;
1148 }
1149
1150 /*****************************************************************************
1151  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1152  *  We don't show the menu directly here because we don't want the
1153  *  caller to block for a too long time.
1154  *****************************************************************************/
1155 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1156                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1157 {
1158     intf_thread_t *p_intf = (intf_thread_t *)param;
1159
1160     if( p_intf->pf_show_dialog )
1161     {
1162         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1163                                 new_val.b_bool, 0 );
1164     }
1165
1166     return VLC_SUCCESS;
1167 }
1168
1169 /*****************************************************************************
1170  * IntfShowCB: callback triggered by the intf-show libvlc variable.
1171  *****************************************************************************/
1172 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1173                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1174 {
1175     intf_thread_t *p_intf = (intf_thread_t *)param;
1176     p_intf->p_sys->p_mi->toggleFSC();
1177
1178     /* Show event */
1179      return VLC_SUCCESS;
1180 }