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